简体   繁体   中英

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

I have the following stored procedure in 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. 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:

SQL query: SET FOREIGN_KEY_CHECKS = ON;

MySQL said: #2014 - Commands out of sync; 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. 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.

It seems that whenever the select statement return no result thanks to the where clause, I receive such an error. 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?

I managed to solve this issue by updating the SELECT query as below:

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; encounter an error whenever SELECT statement returns no result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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