CREATE FUNCTION在本地运行,在服务器上失败-同一版本的MySQL

时间:2018-12-21 03:18:31

标签: mysql mysql-error-1064 create-function

我有一个怪异的东西:我的本地计算机和服务器上都有MySQL v5.7.24。以下代码在本地可以正常执行:

DROP FUNCTION IF EXISTS SharingRatio;

CREATE FUNCTION SharingRatio(users int, sharer int, cabins int, outdoors int) RETURNS DECIMAL(6,2)
    DETERMINISTIC
    READS SQL DATA
BEGIN
    DECLARE ratio DECIMAL(6,2);

    IF cabins IS NULL THEN
     SET cabins = 0;
    END IF;

    IF sharer IS NULL THEN
     SET sharer = 1;
    END IF;

    IF users IS NULL THEN
     SET users = 1;
    END IF;

    SELECT  IF(sharer = 1, telco1, IF(sharer = 2, telco2, IF(sharer = 3, telco3, IF(sharer =4, telco4, IF(sharer = 5, telco5, null)))))
    FROM rates r WHERE (r.type='Utility' and r.cabin = cabins and if(outdoors IS NULL, true, r.outdoor = outdoors) and r.users = users) LIMIT 1 INTO ratio;

    IF ratio IS NULL THEN
       SET ratio = ROUND(100 / users, 2) ;
    END IF;

 RETURN (ratio);
END;


DROP FUNCTION IF EXISTS SharingValue;

CREATE FUNCTION SharingValue(value decimal(10,2), users int, sharer int, cabins int, outdoors int) RETURNS DECIMAL(10,2)
    DETERMINISTIC
    READS SQL DATA
BEGIN
    DECLARE ratio DECIMAL(6,2);

    IF value IS NULL THEN
       RETURN NULL;
    END IF;

    IF cabins IS NULL THEN
     SET cabins = 0;
    END IF;

    IF sharer IS NULL THEN
     SET sharer = 1;
    END IF;

    IF users IS NULL THEN
     SET users = 1;
    END IF;

    SET ratio = SharingRatio(users, sharer, cabins, outdoors);

 RETURN CAST(value * (ratio / 100) AS DECIMAL(10,2));
END;

但是,当我通过具有根访问权限的MySQLAdmin进行部署时,我得到了臭名昭著的 #1064 错误,该错误在“ < / strong>。

我看过许多其他文章,没有悬挂的逗号,没有ELSE IF引起问题。坦白说,我被卡住了!

1 个答案:

答案 0 :(得分:1)

解决了!

存在delimter标签。正确的代码是:

DROP FUNCTION IF EXISTS SharingRatio;
DELIMITER $$
CREATE FUNCTION SharingRatio(users int, sharer int, cabins int, outdoors int)

RETURNS DECIMAL(6,2)
DETERMINISTIC READS SQL DATA

BEGIN
    DECLARE ratio DECIMAL(6,2);

    IF cabins IS NULL THEN
     SET cabins = 0;
    END IF;

    IF sharer IS NULL THEN
     SET sharer = 1;
    END IF;

    IF users IS NULL THEN
     SET users = 1;
    END IF;

    SELECT  IF(sharer = 1, telco1, IF(sharer = 2, telco2, IF(sharer = 3, telco3, IF(sharer =4, telco4, IF(sharer = 5, telco5, null)))))
    FROM rates r WHERE (r.type='Utility' and r.cabin = cabins and if(outdoors IS NULL, true, r.outdoor = outdoors) and r.users = users) LIMIT 1 INTO ratio;

    IF ratio IS NULL THEN
       SET ratio = ROUND(100 / users, 2);
    END IF;

 RETURN (ratio);
END$$

DELIMITER ;

DROP FUNCTION IF EXISTS SharingValue;

DELIMITER $$

CREATE FUNCTION SharingValue(value decimal(10,2), users int, sharer int, cabins int, outdoors int)

RETURNS DECIMAL(10,2)
DETERMINISTIC
READS SQL DATA

BEGIN
    DECLARE ratio DECIMAL(6,2);

    IF value IS NULL THEN
       RETURN NULL;
    END IF;

    IF cabins IS NULL THEN
     SET cabins = 0;
    END IF;

    IF sharer IS NULL THEN
     SET sharer = 1;
    END IF;

    IF users IS NULL THEN
     SET users = 1;
    END IF;

    SET ratio = SharingRatio(users, sharer, cabins, outdoors);

 RETURN CAST(value * (ratio / 100) AS DECIMAL(10,2));
END$$
DELIMITER ;