I can't get the WHILE statement to work in MySQL.
The version of MySQL that I am using is:
Ver 12.16 Distrib 4.0.6-gamma, for Win95/Win98 (i32)
running on Windows MX.
Here is the relevant section from the manual:
20.1.9.7 WHILE Statement
[begin_label:] WHILE search_conditio n DO
statement_list
END WHILE [end_label]
The statement or statements within a WHILE statement are repeated as
long as the search_conditio n is true.
begin_label and end_label must be the same, if both are specified.
For example:
CREATE PROCEDURE dowhile()
BEGIN
DECLARE v1 INT DEFAULT 5;
WHILE v1 > 0 DO
....
SET v1 = v1 - 1;
END WHILE;
END
- - - - - - - - -
I tried to execute the example code - slightly modified:
CREATE PROCEDURE dowhile()
BEGIN
DECLARE v1 INT DEFAULT 5;
WHILE v1 > 0 DO
SET v1 = v1 - 1;
END WHILE;
END
I got the following error message:
"You have an error in your SQL syntax. Check the manual that
corresponds to your MySQL version for the right syntax to use near
‘PROCEDURE dowhile() BEGIN DECLARE v1 INT DEFAULT 5' at line 1."
Then I tried it again with @ in front of the variables:
CREATE PROCEDURE dowhile()
BEGIN
DECLARE @v1 INT DEFAULT 5;
WHILE @v1 > 0 DO
SET @v1 = @v1 - 1;
END WHILE;
END
I got the same error message.
- - - - - - - - -
Apart from the above, I have tried numerous other variations including
table searches.
What am I doing wrong?
Thanks much for your help.
The version of MySQL that I am using is:
Ver 12.16 Distrib 4.0.6-gamma, for Win95/Win98 (i32)
running on Windows MX.
Here is the relevant section from the manual:
20.1.9.7 WHILE Statement
[begin_label:] WHILE search_conditio n DO
statement_list
END WHILE [end_label]
The statement or statements within a WHILE statement are repeated as
long as the search_conditio n is true.
begin_label and end_label must be the same, if both are specified.
For example:
CREATE PROCEDURE dowhile()
BEGIN
DECLARE v1 INT DEFAULT 5;
WHILE v1 > 0 DO
....
SET v1 = v1 - 1;
END WHILE;
END
- - - - - - - - -
I tried to execute the example code - slightly modified:
CREATE PROCEDURE dowhile()
BEGIN
DECLARE v1 INT DEFAULT 5;
WHILE v1 > 0 DO
SET v1 = v1 - 1;
END WHILE;
END
I got the following error message:
"You have an error in your SQL syntax. Check the manual that
corresponds to your MySQL version for the right syntax to use near
‘PROCEDURE dowhile() BEGIN DECLARE v1 INT DEFAULT 5' at line 1."
Then I tried it again with @ in front of the variables:
CREATE PROCEDURE dowhile()
BEGIN
DECLARE @v1 INT DEFAULT 5;
WHILE @v1 > 0 DO
SET @v1 = @v1 - 1;
END WHILE;
END
I got the same error message.
- - - - - - - - -
Apart from the above, I have tried numerous other variations including
table searches.
What am I doing wrong?
Thanks much for your help.
Comment