我已经实现了如下的存储过程,但是当我尝试应用它时,我收到以下错误消息:
错误1064(42000):您的SQL语法出错;检查与MySQL服务器版本对应的手册,以便在'WHERE symbol_id = id GROUP BY symbol_id;
附近使用正确的语法我做了一些调试,发现它是由@max变量引起的,我试图将结果写入,但是我没有看到语法有什么问题,有人可以建议吗?
DROP PROCEDURE IF EXISTS `GENERATE_REPORT`;
DELIMITER $$
CREATE DEFINER=CURRENT_USER PROCEDURE `GENERATE_REPORT`()
BEGIN
DECLARE id INT;
DECLARE max INT;
DECLARE at_end BIT DEFAULT 0;
DECLARE cur CURSOR
FOR SELECT symbol_id from trade;
DECLARE CONTINUE HANDLER
FOR SQLSTATE '02000' SET at_end=1;
OPEN cur;
FETCH cur INTO id;
WHILE (NOT at_end) DO
SELECT SUM(quantity) FROM trade INTO **@max** WHERE symbol_id = id GROUP BY symbol_id;
FETCH cur into id;
END WHILE;
CLOSE cur;
END
$$ DELIMITER;
答案 0 :(得分:1)
SELECT ... INTO
中的语法不正确:
更改
SELECT SUM(quantity)
FROM trade
INTO @max -- Incorrect placement
WHERE symbol_id = id
GROUP BY symbol_id;
要
SELECT SUM(quantity)
INTO @max -- Correct placement
FROM trade
WHERE symbol_id = id
GROUP BY symbol_id;
INTO
应该在SELECT
之后和FROM
之前