2012-03-10 206 views
6

我只是讓我的腳溼存儲過程。據我所看到的教程,這應該是有效的(MySQL的5.5):MySQL:如果存儲過程

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; 

但是,它是引發此錯誤:

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 

哪裏是我的語法錯誤?

謝謝。

回答

11

你缺少一個分號

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; 
+0

謝謝!它應該是顯而易見的! – dotancohen 2012-03-10 20:20:52

5

存儲過程是有點棘手。但這裏是我測試併發布給你的一個例子。在你的例子中,你錯過了幾個分號和最後的「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 $$ 
+0

謝謝。實際上'END'是一種更深入的方式,在解析器甚至還沒有完成的代碼中!但事實上,這是我的問題,缺少分號。 – dotancohen 2012-03-10 21:12:47