MySQL:IF 在存储过程中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9649797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 12:27:54  来源:igfitidea点击:

MySQL: IF in stored procedure

mysqlstored-procedures

提问by dotancohen

I am just getting my feet wet with stored procedures. According to the tutorials that I have seen, this should be valid (MySQL 5.5):

我只是被存储过程弄湿了。根据我看过的教程,这应该是有效的(MySQL 5.5):

CREATE PROCEDURE someFunction ( a VARCHAR(256),  b VARCHAR(256) )
    BEGIN
        DECLARE haveAllVariables INT;
        SET haveAllVariables = 1;

    IF     a = "" THEN SET haveAllVariables = 0
    ELSEIF b = "" THEN SET haveAllVariables = 0
    END IF;

However, it is throwing this error:

但是,它抛出了这个错误:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'ELSEI
F b = "" THEN SET haveAllVariables = 0

Where is the error in my syntax?

我的语法错误在哪里?

Thanks.

谢谢。

回答by Martin.

You're missing a semicolon

你少了一个分号

CREATE PROCEDURE someFunction ( a VARCHAR(256),  b VARCHAR(256) )
    BEGIN
        DECLARE haveAllVariables INT;
        SET haveAllVariables = 1;

    IF     a = "" THEN SET haveAllVariables = 0;
    ELSEIF b = "" THEN SET haveAllVariables = 0;
    END IF;

回答by Ronald Weidner

Stored procedures are a bit tricky. But here is an example I tested and posted for you. In your example you were missing a couple of semicolons and the final "END".

存储过程有点棘手。但这是我为您测试并发布的示例。在您的示例中,您缺少几个分号和最后的“END”。

DELIMITER $$
  CREATE PROCEDURE someFunction ( a VARCHAR(256),  b VARCHAR(256) )
  BEGIN
    DECLARE haveAllVariables INT;
    SET haveAllVariables = 1;

  IF  a = '' THEN 
    SET haveAllVariables = 0;
  ELSEIF b = '' THEN 
    SET haveAllVariables = 0;
  END IF;
END $$