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!

Phrancis
  • 2,222
  • 2
  • 27
  • 40
  • 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 http://stackoverflow.com/questions/10259504/delimiters-in-mysql. Other clients (like PHPMyAdmin) have their own methods of setting an alternate delimiter. – Michael Berkowski May 11 '14 at 21:45

1 Answers1

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 ;
Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63