创建函数时出现MySQL错误

时间:2016-03-30 00:41:06

标签: mysql mysql-error-1064

当我想执行下面的查询创建函数时,我收到此错误,我检查了所有看起来无法找到问题的内容:

  

MySQL说:文档#1064 - 您的SQL中有错误   句法;查看与MySQL服务器版本对应的手册   要在'ELSEIF价格>附近使用正确的语法1000那么       开始         SET last3 = SUBSTRING(price,-3); '第18行

DELIMITER $$


CREATE FUNCTION GetPrice(number decimal(10,2), profit decimal (10,2)) RETURNS INT
    DETERMINISTIC
BEGIN
    DECLARE price int;
    DECLARE last3 int;
    DECLARE rootn int;
    SET price = CEIL(number + (number * profit /100));
    IF price < 1000 THEN
    BEGIN
      IF (price > 0 and price <= 500) THEN
      SET price = 500;
      ELSEIF (price >500 and price <= 750) THEN
      SET price = 750;
      ELSEIF (price >750 and price < 1000) THEN
      SET price = 1000;
      END IF;
    END
    ELSEIF price > 1000 THEN
    BEGIN
      SET last3 = SUBSTRING(price, -3);
      SET rootn = price-last3;
      IF (last3 >= 0 and price <= 100) THEN
      SET last3 = 0;
      ELSEIF (last3 > 100 and price <= 350) THEN
      SET last3 = 250;
      ELSEIF (last3 > 350 and price <= 600) THEN
      SET last3 = 500;
      ELSEIF (last3 > 600 and last3 <= 850) THEN
      SET last3 = 750;
      ELSEIF (last3 > 850 and price <= 1000) THEN
      SET last3 = 1000;
      END IF;
      SET price = rootn + last3;
    END
    ELSEIF price = 10000 THEN
    SET price = 1000;
    END IF;

 RETURN (price);
END$$
DELIMITER;

你能帮帮我吗?

1 个答案:

答案 0 :(得分:2)

你必须用分号结束END

DELIMITER //
CREATE FUNCTION `GetPrice`(number decimal(10,2), profit decimal (10,2)) RETURNS int(11)
    DETERMINISTIC
BEGIN
    DECLARE price int;
    DECLARE last3 int;
    DECLARE rootn int;
    SET price = CEIL(number + (number * profit /100));
    IF price < 1000 THEN
    BEGIN
      IF (price > 0 and price <= 500) THEN
      SET price = 500;
      ELSEIF (price >500 and price <= 750) THEN
      SET price = 750;
      ELSEIF (price >750 and price < 1000) THEN
      SET price = 1000;
      END IF;
    END;                              -- here
    ELSEIF price > 1000 THEN
    BEGIN
      SET last3 = SUBSTRING(price, -3);
      SET rootn = price-last3;
      IF (last3 >= 0 and price <= 100) THEN
      SET last3 = 0;
      ELSEIF (last3 > 100 and price <= 350) THEN
      SET last3 = 250;
      ELSEIF (last3 > 350 and price <= 600) THEN
      SET last3 = 500;
      ELSEIF (last3 > 600 and last3 <= 850) THEN
      SET last3 = 750;
      ELSEIF (last3 > 850 and price <= 1000) THEN
      SET last3 = 1000;
      END IF;
      SET price = rootn + last3;
    END;                                   -- and here
    ELSEIF price = 10000 THEN
    SET price = 1000;
    END IF;

 RETURN (price);
END//
DELIMITER ;

SQL Fiddle