gpt4 book ai didi

mysql - mysql存储过程中的嵌套循环

转载 作者:行者123 更新时间:2023-11-30 23:31:38 26 4
gpt4 key购买 nike

我在存储过程中有一个简单的嵌套 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

谢谢。

最佳答案

试试这个:

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 ;

关于mysql - mysql存储过程中的嵌套循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10298860/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com