问题
I am having a simple nested while loop in stored procedure , but it's giving an error. The loop does not nothing great, just for learning purpose i created two loops.
delimiter $$
create procedure getSum(in input int , out output int)
begin
set output = 0;
while input >= 1 do
declare tmp int default 1;
while tmp <= 5 do
set output = output + input ;
set tmp = tmp + 1;
end while ;
set input = input - 1 ;
end while;
end $$
delimiter ;
Below is the error
#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 'declare tmp int default 1; while tmp <= 5 do set output = output + inpu' at line 7
Thanks.
回答1:
Try this:
delimiter $$
create procedure getSum(in input int , out output int)
begin
declare tmp int default 1;
set output = 0;
while input >= 1 do
set tmp = 1;
while tmp <= 5 do
set output = output + input ;
set tmp = tmp + 1;
end while ;
set input = input - 1 ;
end while;
end $$
delimiter ;
来源:https://stackoverflow.com/questions/10298860/nested-loop-in-mysql-stored-procedure