MySQL stored procedure: OUT parameter not being set

*爱你&永不变心* 提交于 2019-12-10 19:01:18

问题


I've got a stored procedure in MySQL that gets the next unique ID from a table, to use as an ID for 2 other tables (not the best way to do it, I'm sure, but I'm modifying someone else's code here). The procedure is as follows:

DELIMITER $$
CREATE DEFINER=`root`@`%` PROCEDURE `GetNextID`( OUT id bigint )
BEGIN
  DECLARE uid VARCHAR(255);
  SET uid = uuid();
  INSERT INTO `ident_column_generator` (u) VALUES (uid);
  SELECT ID INTO id FROM `ident_column_generator` WHERE u = uid;
  DELETE FROM `ident_column_generator` WHERE u = uid;
END$$

When I call the procedure from MySQL Workbench:

CALL GetNextID( @id );
SELECT @id;

@id is NULL. I can't work out what's going wrong? Even if I run SET @id = 0; before calling the procedure, it ends up as NULL afterwards. If I call the functions within the procedure manually from MySQL Workbench, @id outputs fine, e.g.:

SET @uid = uuid();
INSERT INTO `ident_column_generator` (u) VALUES (@uid);
SELECT ID INTO @id FROM `ident_column_generator` WHERE u = @uid;
DELETE FROM `ident_column_generator` WHERE u = @uid;
SELECT @id;

This outputs @id as being a valid number.

Any ideas why id isn't being set properly?


回答1:


Typically, spent 3 hours on this, then JUST after I posted the question I find the problem. So, for future reference: It appears MySQL is case insensitive where variables are concerned. The ID column name and id variable apparently completely confused it.

I changed the procedure's input parameter name to retId and then it worked perfectly.




回答2:


Thanks Nick, I also had same issue. The column name and variable name were same due to which we were getting the issue.



来源:https://stackoverflow.com/questions/17673866/mysql-stored-procedure-out-parameter-not-being-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!