我已成功创建了以下MySQL SP ..
CREATE DEFINER=`root`@`%` PROCEDURE `Common_Proc_Create_NewId`
(
TableName VARCHAR(250),
ColumnName VARCHAR(150),
OUT ReturnId BIGINT
)
BEGIN
DECLARE varb BIGINT;
SET @NewId:= CONCAT('SELECT (IFNULL(MAX(', ColumnName, '), 0) + 1) INTO ', varb, ' FROM ', TableName);
PREPARE Stmnt FROM @NewId;
EXECUTE Stmnt;
DEALLOCATE PREPARE Stmnt;
SET ReturnId = varb;
END$$
但是当从另一个SP调用时,我收到以下错误:
错误代码:1064您的SQL语法出错;检查 手册,对应右边的MySQL服务器版本 语法在' NULL附近使用'在第1行
致电SP
CREATE DEFINER=`root`@`%` PROCEDURE `Masters_Proc_Create_BranchType`(
BranchTypName VARCHAR(100)
)
BEGIN
CALL Common_Proc_Create_NewId('Masters_BranchType', 'BranchTypeId', @Id);
INSERT INTO Masters_BranchType (BranchTypeId, BranchTypeName) VALUES (@Id, BranchTypName);
SELECT @Id;
END$$
答案 0 :(得分:0)
在您的存储过程Common_Proc_Create_NewId
中,部分into varb
导致了问题,并认为在准备好的声明中不允许这样做(不确定)。而不是你的方式,尝试如下,它工作正常(包括示例代码)
delimiter //
CREATE PROCEDURE dynamic1(IN tbl VARCHAR(64), IN col VARCHAR(64), OUT ret int)
BEGIN
SET @s = CONCAT('SELECT @i := (IFNULL(MAX(', col, '), 0) + 1) FROM ', tbl);
PREPARE stmt FROM @s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
set ret = @i;
END
//
delimiter ;
call dynamic1('test1','col',@id);
select @id;