每次调用这个存储过程我都会得到这个错误:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL' at line 1
似乎是什么问题?
代码如下:
DELIMITER $$
DROP PROCEDURE IF EXISTS `singledb`.`TOUR_TRANSFER`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `TOUR_TRANSFER`()
BEGIN
BEGIN -- for rubric table
DECLARE tblRubric_rubric_id INT;
SET @qry = concat('SELECT count(*) into ',tblRubric_rubric_id,' FROM zerodb2.tour_template');
PREPARE statement FROM @qry;
EXECUTE statement;
SELECT tblRubric_rubric_id;
END;
END$$
DELIMITER ;
最佳答案
来自 http://bugs.mysql.com/bug.php?id=15263 :
"Parameter markers can be used only where data values should appear, not for SQL keywords, identifiers, and so forth."
所以,我们不能这样做:
PREPARE stmt FROM 'SELECT id INTO ? FROM t1';
我们不能用?
代替标识符(变量名)。
当你使用参数名时:
PREPARE stmt FROM 'SELECT id INTO rid FROM t1';
在正在准备的字符串中,服务器根本不知道 rid
在您尝试准备的语句中指的是什么。您可以尝试在 SP 之外准备它,结果相同:
mysql> prepare stmt from 'select id into p from t1';
ERROR 1327 (42000): Undeclared variable: p
所以我只使用了用户变量,见下文:
DROP PROCEDURE IF EXISTS `singledb`.`TOUR_TRANSFER`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `TOUR_TRANSFER`()
BEGIN
BEGIN -- for rubric table
-- DECLARE @tblRubric_rubric_id INT;
SET @tblRubric_rubric_id := 0;
SET @qry = 'SELECT count(*) into @tblRubric_rubric_id FROM zerodb2.tour_template';
PREPARE statement FROM @qry;
EXECUTE statement;
END;
END$$
关于mysql - 在mysql中使用select into局部变量和准备好的语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7917345/