Nested loop in mysql stored procedure

梦想的初衷 提交于 2019-12-11 07:39:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!