- Newest
- Most votes
- Most comments
You need to add a delimiter as explained here
The following works for me when I test it on Aurora MySQL 2.10.0:
delimiter //
CREATE PROCEDURE dowhile()
BEGIN
DECLARE v1 INT DEFAULT 5;
WHILE v1 > 0 DO
SET v1 = v1 - 1;
END WHILE;
END; //
delimiter ;
According to this answer on Stack Overflow, you can only use WHILE if you're inside a stored program or function body.
EDIT: Shiv has correctly identified the problem in relation to a missing delimiter reassignment. A good explanation of why the delimiter is sometimes necessary is given in the MySQL docs:
If you use the mysql client program to define a stored program containing semicolon characters, a problem arises. By default, mysql itself recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause mysql to pass the entire stored program definition to the server.
The example uses // as a temporary delimiter, but any character or multiple characters (except backslash) can be used, for example $$ is commonly seen. The idea is to nest the stored program inside a pair of delimiter statements:
delimiter $$ -- <- set delimiter temorarily to $$ /* stored program definition goes here, including semicolons */ delimeter; -- <- restore delimiter to default (semicolon)
WHILE statement is part of Flow Control Statements
MySQL supports the
IF,CASE,ITERATE,LEAVE LOOP,WHILE, andREPEATconstructs for flow control within stored programs.
Yes. From there I took this sample:
CREATE PROCEDURE dowhile() BEGIN DECLARE v1 INT DEFAULT 5;
WHILE v1 > 0 DO SET v1 = v1 - 1; END WHILE; END;
I and get this error: 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
None of the samples I found works.
Thanks.
Relevant content
- asked a year ago
- asked 6 months ago

Thanks. This solved it. Just want to point out that DELIMITER is needed with some clients. Is not a feature of Aurora or MySQL. For instance when I call the query using PyMySQL, I need to remove them.