同一版本的mysql

yfjy0ee7  于 2021-06-15  发布在  Mysql
关注(0)|答案(1)|浏览(417)

我有一个奇怪的问题:我在本地机器和服务器上安装了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使用root访问权限进行部署时,我得到了臭名昭著的#1064错误,在“”附近的第5行中显示error。
我看过很多其他的帖子,没有悬空的逗号,没有其他会引起问题的帖子。坦白说,我被困住了!

643ylb08

643ylb081#

解决了!
这就是问题所在。正确的代码是:

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 ;

相关问题