我正在为MySQL服务器执行以下SQL脚本。它是创建一个接受课程代码和开始日期作为参数的SPROC,然后在连续的工作日为每个模块添加会话。
DELIMITER $$
CREATE PROCEDURE Assign_Schedule (IN course_code CHAR(3),IN start_date DATE)
BEGIN
DECLARE module_code CHAR(2);
DECLARE day_count TINYINT;
DECLARE new_date DATE;
DECLARE finished BOOLEAN;
DECLARE mod_cursor CURSOR FOR SELECT code FROM module WHERE module.course_code = course_code;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = TRUE;
SET day_count = 0;
SET finished = FALSE;
IF PERIOD_DIFF(EXTRACT(YEAR_MONTH FROM NOW()), EXTRACT(YEAR_MONTH FROM start_date)) > 0 THEN
OPEN mod_cursor;
insert_schedule : LOOP
BEGIN
FETCH NEXT FROM mod_cursor INTO module_code;
IF finished THEN
LEAVE insert_schedule;
END IF;
SET new_date = DATEADD(day, day_count, start_date);
IF LEFT(DATE_FORMAT(new_date,'%W'),1) != 'S' THEN
INSERT INTO session (code, date) VALUES (module_code,new_date);
SET day_count = (day_count + 1);
ELSEIF DATE_FORMAT(new_date,'%w') = 0 THEN
INSERT INTO session (code, date) VALUES (module_code, DATEADD(day, day_count + 1,new_date));
SET day_count = (day_count + 2);
ELSEIF DATE_FORMAT(new_date,'%w') = 6 THEN
INSERT INTO session (code, date) VALUES (module_code, DATEADD(day, day_count + 2,new_date));
SET day_count = (day_count + 3);
END IF;
END LOOP insert_schedule;
CLOSE mod_cursor;
END IF;
END;
$$
我收到以下错误消息:
[Err] 1064 - 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:
'LOOP insert_schedule;
CLOSE mod_cursor;
END IF;
END;
$$'
at line 38
如果有人能告诉我错误在哪里,我会非常感激!欢呼!
答案 0 :(得分:1)
我的猜测是你错过了结束第二次 BEGIN
在这些
之前没有 END 'LOOP insert_schedule;
CLOSE mod_cursor;
END IF;
END;
$$'
DELIMITER $$
CREATE PROCEDURE Assign_Schedule (IN course_code CHAR(3),IN start_date DATE)
BEGIN
DECLARE module_code CHAR(2);
DECLARE day_count TINYINT;
DECLARE new_date DATE;
DECLARE finished BOOLEAN;
DECLARE mod_cursor CURSOR FOR SELECT code FROM module WHERE module.course_code = course_code;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = TRUE;
SET day_count = 0;
SET finished = FALSE;
IF PERIOD_DIFF(EXTRACT(YEAR_MONTH FROM NOW()), EXTRACT(YEAR_MONTH FROM start_date)) > 0 THEN
OPEN mod_cursor;
insert_schedule : LOOP
BEGIN
FETCH NEXT FROM mod_cursor INTO module_code;
IF finished THEN
LEAVE insert_schedule;
END IF;
SET new_date = DATEADD(day, day_count, start_date);
IF LEFT(DATE_FORMAT(new_date,'%W'),1) != 'S' THEN
INSERT INTO session (code, date) VALUES (module_code,new_date);
SET day_count = (day_count + 1);
ELSEIF DATE_FORMAT(new_date,'%w') = 0 THEN
INSERT INTO session (code, date) VALUES (module_code, DATEADD(day, day_count + 1,new_date));
SET day_count = (day_count + 2);
ELSEIF DATE_FORMAT(new_date,'%w') = 6 THEN
INSERT INTO session (code, date) VALUES (module_code, DATEADD(day, day_count + 2,new_date));
SET day_count = (day_count + 3);
END IF;
END;
END LOOP insert_schedule;
CLOSE mod_cursor;
END IF;
END;
$$