Mysql语法错误创建存储函数5.6.10

时间:2018-05-10 19:32:03

标签: mysql stored-functions

我在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 ;

我得到错误:[查询2中的错误]您的SQL语法中有错误;检查与您的MySQL服务器版本对应的手册,以获得在' RETURN天附近使用的正确语法; END'在第13行

1 个答案:

答案 0 :(得分:1)

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 ;