简体   繁体   English

使用select语句时无结果的MySql存储过程通过命令不同步

[英]MySql stored procedure through Commands out of sync when using select statement with no result

I have the following stored procedure in MySql 我在MySql中具有以下存储过程

CREATE PROCEDURE fileAdd (OUT `file_id` INT UNSIGNED, IN `mother_id` INT UNSIGNED, IN `input_filename` VARCHAR(255), IN `input_dte` INT UNSIGNED, IN `input_user_id` SMALLINT UNSIGNED, IN `input_type` TINYINT(1) UNSIGNED)
BEGIN

SET @r = 1;
SET @u_id = 0;

SELECT @r:=rgt, @u_id:=user_id FROM filemanager_files WHERE id=mother_id;

IF @u_id = 0 THEN 
SET @u_id = input_user_id;
END IF;

UPDATE filemanager_files SET lft = lft + 2 WHERE lft >= @r;
UPDATE filemanager_files SET rgt = rgt + 2 WHERE rgt >= @r;

INSERT INTO filemanager_files (rgt, lft, filename, dte_created, dte_updated, user_id, type) VALUES (@r+1, @r, input_filename, input_dte, input_dte, @u_id, input_type);

SET file_id = LAST_INSERT_ID();

END

The main input variable is mother_id that makes me a problem. 主要输入变量是mother_id,这使我遇到了问题。 When I call it with the parameter mother_id=0 the statements inside work fine and a new row is inserted into the table but I get the following error: 当我使用参数mother_id = 0调用它时,内部语句运行良好,并且在表中插入了新行,但出现以下错误:

SQL query: SET FOREIGN_KEY_CHECKS = ON; SQL查询:SET FOREIGN_KEY_CHECKS = ON;

MySQL said: #2014 - Commands out of sync; MySQL说:#2014-命令不同步; you can't run this command now 您现在不能运行此命令

surprisingly when I call the same procedure with the parameter mother_id="A_VALID_ROW_ID" everything works just fine and I receive no errors. 令人惊讶的是,当我使用参数mother_id =“ A_VALID_ROW_ID”调用相同的过程时,一切正常,并且我没有收到任何错误。 I removed the following statement from my procedure 我从程序中删除了以下语句

SELECT @r:=rgt, @u_id:=user_id FROM filemanager_files WHERE id=mother_id;

I tested it again with parameter mother_id = 0 and this time I received no error. 我再次使用参数mother_id = 0对其进行了测试,这次我没有收到任何错误。

It seems that whenever the select statement return no result thanks to the where clause, I receive such an error. 似乎每当select语句由于where子句而没有返回任何结果时,我会收到这样的错误。 My procedure has default values for @r and @u_id variables in the case that following SELECT statement returns no result so why should I receive such an error? 在以下SELECT语句未返回结果的情况下,我的过程具有@r和@u_id变量的默认值,为什么我应该收到这样的错误?

I managed to solve this issue by updating the SELECT query as below: 我设法通过更新SELECT查询来解决此问题,如下所示:

SELECT rgt, user_id INTO @r, @u_id FROM filemanager_files WHERE id=mother_id;

I yet don't understand why using @r:=rgt, @u_id:user_id instead; 我还不明白为什么要使用@r:= rgt,@u_id:user_id; encounter an error whenever SELECT statement returns no result. SELECT语句不返回结果时遇到错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM