gpt4 book ai didi

MySQL 语法错误 : I just can't find it

转载 作者:行者123 更新时间:2023-11-29 13:17:28 25 4
gpt4 key购买 nike

我正在为 MySQL 服务器执行以下 SQL 脚本。它是创建一个接受类(class)代码和开始日期作为参数的 SPROC,然后在连续的工作日为每个模块添加类(class)。

    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

如果有人能告诉我错误在哪里,我将非常感激,干杯!

最佳答案

我猜你错过了第二个BEGINEND

这些之前没有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;
$$

关于MySQL 语法错误 : I just can't find it,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21286607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com