1
我在存储过程中有一个简单的嵌套while循环,但它给出了一个错误。 循环没有什么了不起,只是为了学习目的我创建了两个循环。mysql存储过程中的嵌套循环
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
感谢。
什么是错我的代码?我每次都必须声明局部变量吗? – 2012-04-24 13:34:08
我将'declare'移动到块的开头,并添加了'set tmp = 1;'以与您初始化的值保持一致。 – Ryan 2012-04-24 13:36:28
局部变量是相对于'BEGIN ... END'块定义的。通常,在块的开头定义它们可以消除像这样的错误。我认为这是一个要求,在块中定义它们的第一件事,就像在旧的过程语言中一样......但我可能是错的。 – Ryan 2012-04-24 13:41:39