使用PHPMyAdmin和MySQL。 我正在尝试添加例程。
BEGIN
DECLARE cursor_ID INT;
DECLARE cursor_VAL VARCHAR;
DECLARE done INT DEFAULT FALSE;
DECLARE cursor_i CURSOR FOR SELECT ca.apprentice_id, c.courseNumber, c.level FROM tblcourseassignments ca LEFT JOIN tblcourses c ON ca.course_id = c.id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
DECLARE varc1 INT DEFAULT 0;
DECLARE varc2 INT DEFAULT 0;
DECLARE varc3 INT DEFAULT 0;
DECLARE gradeCount INT;
OPEN cursor_i;
read_loop: LOOP
FETCH cursor_i INTO varc1, varc2;
SELECT COUNT(id) INTO gradeCount FROM tblapprenticegrades WHERE apprentice_id = varc1 and course_id = varc2;
if gradeCount = 0 then
INSERT INTO tblapprenticegrades(apprentice_id, course_id, phase) VALUES (varc1, varc2, varc3);
end if
IF done THEN
LEAVE read_loop;
END IF;
--queries which couldnt be made into set based queries go here---
END LOOP;
CLOSE cursor_i;
END;
但我继续犯这个错误:
MySQL说:#1064 - 你的SQL语法有错误;检查 手册,对应右边的MySQL服务器版本 在'附近使用的语法'; DECLARE完成INT DEFAULT FALSE; DECLARE cursor_i CURSOR FOR SELECT ca.ap'在第3行
答案 0 :(得分:0)
请在代码中找到修复程序。
BEGIN
DECLARE cursor_ID INT;
DECLARE cursor_VAL VARCHAR(100); /* Varchar length */
DECLARE done INT DEFAULT FALSE;
DECLARE varc1 INT DEFAULT 0;
DECLARE varc2 INT DEFAULT 0;
DECLARE varc3 INT DEFAULT 0;
DECLARE gradeCount INT;
/* cursor and handler declaration at the end of declarations */
DECLARE cursor_i CURSOR FOR SELECT ca.apprentice_id, c.courseNumber, c.`level` FROM tblcourseassignments ca LEFT JOIN tblcourses c ON ca.course_id = c.id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cursor_i;
read_loop: LOOP
FETCH cursor_i INTO varc1, varc2;
SELECT COUNT(id) INTO gradeCount FROM tblapprenticegrades WHERE apprentice_id = varc1 and course_id = varc2;
if gradeCount = 0 then
INSERT INTO tblapprenticegrades(apprentice_id, course_id, phase) VALUES (varc1, varc2, varc3);
end if; /* semicolon here */
IF done THEN
LEAVE read_loop;
END IF;
--queries which couldnt be made into set based queries go here---
END LOOP;
CLOSE cursor_i;
END