在phpmyadmin中创建一个功能失败 - 为什么?

时间:2015-01-27 10:26:41

标签: mysql phpmyadmin

这是我正在运行的SQL。它在我的本地SQL环境中运行没有问题。

CREATE FUNCTION AddWorkDays
(
    WorkingDays INT,
    StartDate DATETIME
)
RETURNS DATETIME

BEGIN
DECLARE Count INT;
DECLARE i INT;
DECLARE NewDate DATETIME;
SET Count = 0;
SET i = 0;

WHILE (i < WorkingDays) DO
    BEGIN
        SELECT Count + 1 INTO Count;
        SELECT i + 1 INTO i;
        WHILE DAYOFWEEK(DATE_ADD(StartDate,INTERVAL Count DAY)) IN (1,7) DO
            BEGIN
                SELECT Count + 1 INTO Count;
            END;
        END WHILE;
    END;
END WHILE;

SELECT DATE_ADD(StartDate,INTERVAL Count DAY) INTO NewDate;
RETURN NewDate;

END;

在phpmyadmin中运行它给了我:

  

您的SQL语法出错;检查与MySQL服务器版本对应的手册,以获得正确的语法   靠近&#39;&#39;第9行

第9行是:

DECLARE Count INT;

1 个答案:

答案 0 :(得分:0)

你的代码似乎在phpmyadmin和MySQLWorkbench中工作得很好,或者你添加定义器...

CREATE DEFINER=`root`@`localhost` FUNCTION `AddWorkDays`(
    WorkingDays INT,
    StartDate DATETIME
) 

RETURNS datetime
BEGIN
    DECLARE Count INT;
    DECLARE i INT;
    DECLARE NewDate DATETIME;
    SET Count = 0;
    SET i = 0;

WHILE (i < WorkingDays) DO
    BEGIN
        SELECT Count + 1 INTO Count;
        SELECT i + 1 INTO i;
        WHILE DAYOFWEEK(DATE_ADD(StartDate,INTERVAL Count DAY)) IN (1,7) DO
            BEGIN
                SELECT Count + 1 INTO Count;
            END;
        END WHILE;
    END;
END WHILE;

SELECT DATE_ADD(StartDate,INTERVAL Count DAY) INTO NewDate;
RETURN NewDate;

END;