17

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):

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.

2 Answers 2

29

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! It should have been obvious!
11

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".

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 $$

1 Comment

Thank you. Actually the END is quite a way further down, in code that the parser didn't even get to yet! But it was in fact the missing semicolon that was my problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.