我想为 MySQL 创建一个存储例程,计算一个月的营业日数或工作日数(工作日为周一至周五)。
这是一个语法错误,但我不知道语法错误是什么。它告诉我的只是:
1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 2 行的 'WHILE(@daycount < @totaldays) DO IF (WEEKDAY(@checkweekday) < 6) THEN' 附近使用正确的语法
我的语法错误如下:
WHILE(@daycount < @totaldays) DO
IF (WEEKDAY(@checkweekday) < 6) THEN
我的代码:
SELECT MONTH(CURDATE()) INTO @curmonth;
SELECT MONTHNAME(CURDATE()) INTO @curmonthname;
SELECT DAY(LAST_DAY(CURDATE())) INTO @totaldays;
SELECT FIRST_DAY(CURDATE()) INTO @checkweekday;
SELECT DAY(@checkweekday) INTO @checkday;
SET @daycount = 0;
SET @workdays = 0;
BEGIN
WHILE(@daycount < @totaldays) DO
IF (WEEKDAY(@checkweekday) < 6) THEN
SET @workdays = @workdays+1;
END IF;
SET @daycount = @daycount+1;
SELECT ADDDATE('@checkweekday', INTERVAL 1 DAY) INTO @checkweekday;
END WHILE;
END;
SELECT @workdays;
有人可以提供帮助吗?
更新 我收到以下代码的相同错误,因此它可能与此有关:
SET @workdays = 0;
IF (WEEKDAY('2013-06-13') < 6) THEN
SET @workdays = @workdays+1;
END IF;
SELECT @workdays;