mysql存储过程中的嵌套循环

时间:2012-04-24 13:28:18

标签: mysql stored-procedures while-loop

我在存储过程中有一个简单的嵌套while循环,但是它给出了一个错误。 循环并不是什么好事,只是为了学习目的,我创建了两个循环。

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 ;

以下是错误

#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

感谢。

1 个答案:

答案 0 :(得分:0)

试试这个:

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 ;