SQL查询:文档
DELIMITER //
CREATE TRIGGER `test` AFTER INSERT ON `test_series`
FOR EACH ROW
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE ids INT;
set ids=1;
DECLARE cur CURSOR FOR SELECT NEW.no_of_exam
FROM test_series WHERE serise_id = NEW.series_id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur;
ins_loop:
LOOP FETCH cur INTO ids;
IF done THEN LEAVE ins_loop;
END IF;
INSERT INTO test_details (series_id,test_no) VALUES(new.series_id,);
ids=ids+1 END LOOP;
CLOSE cur;
END;
//
MySQL说:文档
#1064 - 您的SQL语法出错;查看与您的MySQL服务器版本相对应的手册,以便在#DEC; DECLARE附近使用正确的语法CURSOR FOR SELECT NEW.no_of_exam FROM test_series WHERE serise_id ='在第7行
答案 0 :(得分:0)
无论何时何地,您都可以DECLARE
。所有DECLARE
语句必须位于BEGIN
块之上。
在您的代码中,在某些DECLARE
语句之间,有一个set ids=1;
语句。它违反了DECLARE
要求。
请在代码中进行以下更改。
BEGIN
-- all declares should be just below a 'begin'
DECLARE done INT DEFAULT FALSE;
DECLARE ids INT;
DECLARE cur CURSOR
FOR SELECT NEW.no_of_exam FROM test_series
WHERE serise_id = NEW.series_id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
-- now start your biz logic after all 'declare' statements.
SET ids=1;
OPEN cur;
ins_loop: LOOP
FETCH cur INTO ids;
IF done THEN
LEAVE ins_loop;
END IF;
INSERT INTO test_details (series_id,test_no) VALUES(new.series_id,);
SET ids=ids+1;
END LOOP;
CLOSE cur;
END;
BTW:您正在将光标读入变量但未使用!为什么?我相信你想在插入语句中将它用作test_no
。如果是,那么insert
语句需要跟随更改:
INSERT INTO test_details (series_id,test_no) VALUES(new.series_id, ids);
文档参考:
仅在DECLARE
复合语句中允许
BEGIN ... END
并且必须在任何其他陈述之前开始。声明必须遵循一定的顺序。游标声明必须 出现在处理程序声明之前。变量和条件 声明必须出现在游标或处理程序声明之前