我想在MySQL / MariaDB中创建一个过程,但是我得到了一个我不理解的语法错误。
DELIMITER $$
DROP PROCEDURE IF EXISTS proc_loop$$;
CREATE PROCEDURE proc_loop()
BEGIN
DECLARE i INT;
DECLARE j INT;
DECLARE n INT;
DECLARE c VARCHAR(3);
SET i = 1;
SET j = 1;
SELECT COUNT(*) INTO n FROM AnswerSets;
WHILE i < n DO
WHILE j < 89 DO
SELECT CONCAT("Q", j) INTO c;
INSERT INTO T_ANSWER_SET (U_ID, Q_ID, ANSWER) SELECT i, j, c FROM AnswerSets WHERE Id = i;
SET j = j + 1;
END WHILE;
SET i = i + 1;
END WHILE;
END$$;
DELIMITER ;
我的错误讯息:
ERROR 1064 (42000) at line 16: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ';
CREATE PROCEDURE proc_loop()
BEGIN
DECLARE i INT;
DECLARE j INT;
DECLARE n ' at line 1
提前感谢您的帮助。
答案 0 :(得分:1)
试试这个:
DROP PROCEDURE IF EXISTS proc_loop;
DELIMITER $$
CREATE PROCEDURE `proc_loop`()
BEGIN
DECLARE i INT;
DECLARE j INT;
DECLARE n INT;
DECLARE c VARCHAR(3);
SET i = 1;
SET j = 1;
SELECT COUNT(*) INTO n FROM AnswerSets;
WHILE i < n DO
WHILE j < 89 DO
SELECT CONCAT("Q", j) INTO c;
INSERT INTO T_ANSWER_SET (U_ID, Q_ID, ANSWER) SELECT i, j, c FROM AnswerSets WHERE Id = i;
SET j = j + 1;
END WHILE;
SET i = i + 1;
END WHILE;
END$$
DELIMITER ;
如果这不起作用,作为一种调试手段,尝试删除所有的proc内容,转而支持像select 1;
这样的小事,并查看错误是否仍然存在。由于proc主体中存在轻微的语法错误,我之前看到此错误报告错误。
要调试插入内容,请暂时注释掉
insert into t_answer_set
:
...
SELECT CONCAT("Q", j) INTO c;
-- INSERT INTO T_ANSWER_SET (U_ID, Q_ID, ANSWER)
SELECT i, j, c FROM AnswerSets WHERE Id = i;
...