使用MySQL,我试图创建一个存储过程来检索任何给定表中varchar列的最大值。我想将列的值增加1,然后使用它来存储其他字段。我不希望该列为int并自动递增。
我现在拥有的存储过程是 DELIMITER $$
use gounicartdb$$
DROP PROCEDURE IF EXISTS sp_getUpdatedIDFromTable$$
CREATE PROCEDURE sp_getUpdatedIDFromTable(
IN tableName varchar(50),
IN columnName varchar(50),
IN incrementValue int/*,
OUT updatedID varchar(10)*/
)
BEGIN
SET @newID = "abc";
SET @cmdString = concat("Select max(", columnName, ") from ", tableName);
PREPARE stmt FROM @cmdString;
SELECT @newID = EXECUTE stmt;
END$$
DELIMITER ;
编译时我看不到任何错误,但在执行该过程时会发生以下错误。
14:50:48 Call sp_getUpdatedIDFromTable("user", "SNo", 1) Error Code: 1054. Unknown column 'EXECUTE' in 'field list' 0.000 sec
请帮忙。
答案 0 :(得分:1)
您可以在程序中替换
SET @cmdString = concat("Select max(", columnName, ") into @newID from ", tableName);
PREPARE stmt FROM @cmdString;
EXECUTE stmt;
SELECT @newID;