这是我正在运行的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;


结束;

在phpmyadmin中运行它可以给我:


  您的SQL语法有误。查看与您的MySQL服务器版本相对应的手册以使用正确的语法
  第9行的“”附近


第9行是:

DECLARE Count INT;

最佳答案

您的代码似乎在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;


结束;

关于mysql - 在phpmyadmin中创建功能失败-为什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28168196/

10-11 12:45