mysql创建存储函数时语法错误5.6.10

gxwragnw  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(303)

我在mysql中执行以下函数时遇到语法错误。

DELIMITER $$

DROP FUNCTION IF EXISTS `WORKDAYS` $$
CREATE FUNCTION `WORKDAYS` (start_date DATETIME, end_date DATETIME) 
RETURNS INT
LANGUAGE SQL
DETERMINISTIC

BEGIN 
 DECLARE days INT; 
 SET days = 0;
 REPEAT
 SET days = days + (CASE WHEN DAYNAME(start_date) in ('Friday', 
                      'Saturday', 'Sunday') THEN 0  ELSE 1 END);
 SET start_date = DATE_ADD(start_date, INTERVAL 1 DAY);
 UNTIL start_date > end_date END  REPEAT;     
 RETURN days;
END $$
DELIMITER ;

error i get:[error in query 2]您的sql语法有错误;查看与您的mysql服务器版本对应的手册,了解使用“return days”附近的正确语法;在第13行结束

e37o9pze

e37o9pze1#

查询中隐藏的字符 END REPEAT; . 确保编辑器是utf-8。

DELIMITER $$

DROP FUNCTION IF EXISTS `WORKDAYS`$$

CREATE FUNCTION `WORKDAYS` (start_date DATETIME, end_date DATETIME) 
RETURNS INT
LANGUAGE SQL
DETERMINISTIC

BEGIN 
    DECLARE days INT; 
    SET days = 0;
    REPEAT
        SET days = days + (CASE WHEN DAYNAME(start_date) in ('Friday','Saturday', 'Sunday') THEN 0 ELSE 1 END);
        SET start_date = DATE_ADD(start_date, INTERVAL 1 DAY);
    UNTIL start_date > end_date END REPEAT;
RETURN days;
END$$

DELIMITER ;

相关问题