使用IF-END IF定义MySQL函数时出错

时间:2015-12-22 07:50:02

标签: mysql sql function if-statement

我正在尝试在MySQL中编写一个函数,这将在两天内给我区别,不包括周末和自定义假期。我的END IF语句以红色突出显示,表示语法错误。任何人都可以帮忙吗?

CREATE 
FUNCTION `GetWorkingDays_FromDateRange`(date1 DATE, date2 DATE) RETURNS int(11)
BEGIN

DECLARE dateOne  DATE;
DECLARE dateTwo  DATE;

IF date1>date2  
    THEN
        SET dateTwo = date1;
        SET dateOne = date2;
    ELSE
        SET dateTwo = date2;
        SET dateOne = date1;
END IF;

DECLARE i INT;
SET i = (SELECT COUNT(*) from holidays where calendar_date BETWEEN dateOne AND dateTwo);

RETURN ABS(DATEDIFF(dateTwo, dateOne)) + 1
     - ABS(DATEDIFF(ADDDATE(dateTwo, INTERVAL 1 - DAYOFWEEK(dateTwo) DAY),
                    ADDDATE(dateOne, INTERVAL 1 - DAYOFWEEK(dateOne) DAY))) / 7 * 2
     - (DAYOFWEEK(IF(dateOne < dateTwo, dateOne, dateTwo)) = 1)
     - (DAYOFWEEK(IF(dateOne > dateTwo, dateOne, dateTwo)) = 7)
     - i;
END

1 个答案:

答案 0 :(得分:1)

将所有 DECLARE 语句放在任何其他语句之前。

试试这个:

No changes detected.