MySQL 错误 2014 - 命令不同步 - 尝试调用连续存储过程时
我有一个使用嵌套集模型的分层 MySQL 表设置。我创建了一个存储过程,它允许我删除组织。
当我尝试连续调用存储过程时,出现以下涉及第二个 CALL 命令的错误:
2014 - 命令不同步;您现在无法运行此命令
这是我正在运行的 SQL:
SELECT @id := id,
@parent_id := parent_id
FROM organization
WHERE name = 'TESTDEAL';
CALL deleteOrg(@id);
CALL deleteOrg(@parent_id);
这是我的存储过程:
DELIMITER $$
CREATE PROCEDURE deleteOrg(IN IN_ID INT)
MODIFIES SQL DATA
BEGIN
START TRANSACTION;
SELECT @org_id := id,
@myLeft := lft,
@myRight := rgt,
@myWidth := rgt - lft + 1
FROM organization
WHERE id = IN_ID;
# delete statements for removing roles and other dependencies
DELETE FROM organization
WHERE id = @org_id;
UPDATE organization
SET rgt = rgt - @myWidth
WHERE rgt > @myRight;
UPDATE organization
SET lft = lft - @myWidth
WHERE lft > @myRight;
COMMIT;
END;
$$
DELIMITER ;
为什么第二个命令会失败? MySQL 是否在第一个 CALL 命令提交之前尝试执行第二个 CALL 命令?有没有办法在调用第二个之前强制第一个完全执行?
编辑:组织表正在使用 InnoDB
编辑:尝试删除 START TRANSACTION;并提交;但我仍然遇到同样的错误
I have a hierarchical MySQL table setup using the Nested Set model. I have created a stored proc which allows me to delete an organization.
When I try making consecutive calls to the stored proc, I get the following error referring to the second CALL command:
2014 - Commands out of sync; you can't run this command now
Here is the SQL I am running:
SELECT @id := id,
@parent_id := parent_id
FROM organization
WHERE name = 'TESTDEAL';
CALL deleteOrg(@id);
CALL deleteOrg(@parent_id);
And here is my Stored Procedure:
DELIMITER $
CREATE PROCEDURE deleteOrg(IN IN_ID INT)
MODIFIES SQL DATA
BEGIN
START TRANSACTION;
SELECT @org_id := id,
@myLeft := lft,
@myRight := rgt,
@myWidth := rgt - lft + 1
FROM organization
WHERE id = IN_ID;
# delete statements for removing roles and other dependencies
DELETE FROM organization
WHERE id = @org_id;
UPDATE organization
SET rgt = rgt - @myWidth
WHERE rgt > @myRight;
UPDATE organization
SET lft = lft - @myWidth
WHERE lft > @myRight;
COMMIT;
END;
$
DELIMITER ;
Why would the second command be failing? Is MySQL trying to execute the second CALL command before the first one has been committed? Is there a way I can force the first one to fully execute before calling the second?
EDIT: organization table is using InnoDB
EDIT: Tried removing START TRANSACTION; and COMMIT; but am still getting same error
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该错误似乎仅在从 phpMyAdmin 导入 SQL 脚本时发生。从命令行导入脚本或使用命令行不会导致错误。我假设这个错误与 phpMyAdmin 而不是 MySQL 有关。 (我还看到 phpMyAdmin 与存储过程相关的其他奇怪行为 - 我正在使用 phpMyAdmin v3.3.2)
The error only seems to be occurring when importing an SQL script from phpMyAdmin. Importing the script from the command line or using the command line does not result in an error. I'm assuming this error has to do with phpMyAdmin and not MySQL. (I have also seen other strange behaviour from phpMyAdmin relating to Stored Procedures - I am using phpMyAdmin v3.3.2)
我也在寻找答案,据我所知,它与第一个 SP 返回的记录集有关。您必须“读取”记录集以清空它,然后进行新的 CALL。
I'm looking for the answer too, AFAIK it's related to the recordset returned by the first SP. You must 'read' the recordset to empty it and then make a new CALL.
我猜这可能与您分配变量的方式有关。
我的建议是在过程中声明局部变量,并使用 SELECT...INTO 分配它们。一般来说,这是一个很好的做法,可能会帮助您解决问题。
您的程序如下所示:
I'm guessing this might have something to do with the way you are assigning your variables.
My advice is to declare local variables inside your procedure, and to assign them using
SELECT...INTO
. This is a good practice in general, and may help you with your problem.Here's what your procedure would look like: