Reputation: 1154
I've got an incredibly simple statement that won't work for some reason.
I'm trying to do a FOR loop:
DECLARE counter INT DEFAULT 2;
WHILE counter < 8
....
SET counter = counter + 1;
Incredibly basic, yet I get an error when trying to run the DECLARE
statement. I tried using a user variable, which worked but then the WHILE statement wouldn't work
SET @counter = 2;
WHILE @counter <8
....
SET @counter = @counter + 1;
Is there a system variable that I may have changed that would cause this sort of behavior? This is really frustrating, as everywhere I look indicates I'm doing it correctly. I can literally copy paste code from other solutions and it won't work for me.
Upvotes: 0
Views: 224
Reputation: 307
Is your code inside of a stored procedure? If not, you'll have to create a stored procedure to use these features.
More generally, what problem are you trying to solve? Often, the better solution in a database environment looks more like a query than like a loop.
The documentation for DECLARE
indicates it must be used between begin...end
, and the documentation for begin
...end
is used inside stored procedures and functions.
Also, the syntax for WHILE
statements is:
...
WHILE (some_condition) DO
some_commands
END WHILE
...
You're missing the DO
and END WHILE
Upvotes: 3