当我没有使用out变量时,存储过程正常工作,但是当我用out变量执行存储过程时,会出现此错误:
MySQL说:#1327 - 未声明的变量:sp_result
代码:
CREATE DEFINER=`root`@`localhost` PROCEDURE `test3`(OUT `sp_result` INT(11), IN `sp_where_param` VARCHAR(100), IN `sp_where_value` VARCHAR(100), IN `sp_table_name` VARCHAR(100))
NO SQL
BEGIN
DECLARE tbl_id VARCHAR(100);
SET tbl_id = CONCAT(sp_table_name,'_id');
SET @temp1=CONCAT('SELECT count(',tbl_id,') INTO sp_result FROM ',sp_table_name,' WHERE ',sp_where_param,' = \'',sp_where_value,'\'');
PREPARE stmt1 FROM @temp1;
EXECUTE stmt1;
END
也许没有变量也不起作用:(
答案 0 :(得分:1)
尝试使用用户变量 -
CREATE DEFINER = 'root'@'localhost'
PROCEDURE test3 (OUT `sp_result` int(11), IN `sp_where_param` varchar(100), IN `sp_where_value` varchar(100), IN `sp_table_name` varchar(100))
NO SQL
BEGIN
DECLARE tbl_id varchar(100);
SET tbl_id = CONCAT(sp_table_name, '_id');
SET @temp1 = CONCAT('SELECT COUNT(', tbl_id, ') INTO @sp_result FROM ', sp_table_name, ' WHERE ', sp_where_param, ' = \'', sp_where_value, '\'');
PREPARE stmt1 FROM @temp1;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
set sp_result = @sp_result;
END
...并添加DEALLOCATE PREPARE语句。
答案 1 :(得分:0)
应该是 -
SET tbl_id = CONCAT(sp_table_name,'_id');
SET @temp1=CONCAT('SELECT count(',tbl_id,') INTO
像这样
SET @tbl_id:= CONCAT(sp_table_name,'_id');
SET @temp1:=CONCAT('SELECT count(',tbl_id,') INTO