4

I'm working through learning MySQL (v 5.6) and trying to get a simple WHILE loop to go through. I even just straight copy & paste from the manual (with added SELECT v1; statement).

CREATE PROCEDURE dowhile()
BEGIN
  DECLARE v1 INT DEFAULT 5;

  WHILE v1 > 0 DO
    SELECT v1;
    SET v1 = v1 - 1;
  END WHILE;
END;

Workbench is giving me this error:

CREATE PROCEDURE dowhile() BEGIN DECLARE v1 INT DEFAULT 5 Error Code: 1064. 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 '' at line 3 0.001 sec

Any insight from more experienced MySQL programmers is very appreciated!

1
  • What MySQL client are you using? (edit - I see it's Workbench) If you are using the command line client, you'll need to set the DELIMITER. See stackoverflow.com/questions/10259504/delimiters-in-mysql. Other clients (like PHPMyAdmin) have their own methods of setting an alternate delimiter. Commented May 11, 2014 at 21:45

1 Answer 1

7

The procedure looks ok I guess you are missing the delimiter

delimiter //
CREATE PROCEDURE dowhile()
  BEGIN
   DECLARE v1 INT DEFAULT 5;
    WHILE v1 > 0 DO
     SET v1 = v1 - 1;
   END WHILE;
 END; //
delimiter ;
Sign up to request clarification or add additional context in comments.

Comments

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.