需要帮助检查和修改我的 SQL 代码,旨在找到下一个工作日期(我写了这段代码但收到语法错误消息,寻求帮助)
Need help to check and modify my SQL code which is aimed to find the next working date ( I wrote this code but got syntax error message, ask for help)
CREATE FUNCTION NewStartDate (startdate DATE)
RETURNS DATE DETERMINISTIC
BEGIN
DECLARE nextdate DATE;
DECLARE counter integer;
SET COUNTER = 0
SET nextdate = startdate
WHILE (nextdate in (select `dt` from `calendar2` where `IsHoliday`=1) LOOP
nextdate = DATE_ADD(startdate,INTERVAL counter)
counter=counter+1
END LOOP
RETURN nextdate;
END
您的 SQL 语法有误;查看与您的 MySQL 服务器版本对应的手册,了解在第 9
行 'loop nextdate = DATEADD(nextdate,counter); IF nextdate in (select dt
FROM `c' 附近使用的正确语法
恐怕您提供的代码有不止一个语法错误。值得注意的是,您没有在 date_add 函数 DATE_ADD(startdate,INTERVAL counter)
中指定间隔的单位。我已经在 workbench 中修改了您的代码并对其进行了测试(使用不同的 while 条件)。现在可以使用了。
delimiter //
drop function if exists NewStartDate//
CREATE FUNCTION NewStartDate (startdate DATE)
RETURNS DATE DETERMINISTIC
BEGIN
DECLARE nextdate DATE;
DECLARE counter integer;
SET COUNTER = 0 ;
SET nextdate = startdate ;
WHILE (nextdate in (select `dt` from `calendar2` where `IsHoliday`=1)) do
set nextdate = DATE_ADD(startdate,INTERVAL + counter day);
set counter=counter+1;
END WHILE ;
RETURN nextdate;
END //
CREATE FUNCTION NewStartDate (startdate DATE)
RETURNS DATE DETERMINISTIC
BEGIN
DECLARE nextdate DATE;
DECLARE counter integer;
SET COUNTER = 0
SET nextdate = startdate
WHILE (nextdate in (select `dt` from `calendar2` where `IsHoliday`=1) LOOP
nextdate = DATE_ADD(startdate,INTERVAL counter)
counter=counter+1
END LOOP
RETURN nextdate;
END
您的 SQL 语法有误;查看与您的 MySQL 服务器版本对应的手册,了解在第 9
行 'loop nextdate = DATEADD(nextdate,counter); IF nextdate in (selectdt
FROM `c' 附近使用的正确语法
恐怕您提供的代码有不止一个语法错误。值得注意的是,您没有在 date_add 函数 DATE_ADD(startdate,INTERVAL counter)
中指定间隔的单位。我已经在 workbench 中修改了您的代码并对其进行了测试(使用不同的 while 条件)。现在可以使用了。
delimiter //
drop function if exists NewStartDate//
CREATE FUNCTION NewStartDate (startdate DATE)
RETURNS DATE DETERMINISTIC
BEGIN
DECLARE nextdate DATE;
DECLARE counter integer;
SET COUNTER = 0 ;
SET nextdate = startdate ;
WHILE (nextdate in (select `dt` from `calendar2` where `IsHoliday`=1)) do
set nextdate = DATE_ADD(startdate,INTERVAL + counter day);
set counter=counter+1;
END WHILE ;
RETURN nextdate;
END //