Mysql5.7下的三种循环

方法一:while .....do.....end while
create procedure pwhile()
begin
declare i int;
set i = 0;
while i <10 do
insert into table_name values (i);
set i = i+1;
end while;
end;

call pwhile();

方法二:repeat ....until... end repeat
create procedure prepeat()
begin
declare i int ;
set i =0;
repeat
insert into table_name values(i);
set i +=1;
until i <20;
end repeat;
end;

call prepeat();

方法三:loop ....end loop
create procedure ploop()
begin
declare i int;
set i = 0;
loop_lable:loop
insert into table_name values (i);
set i = i+1;
if i >=20 then
leave loop_table;
end if ;
end loop;
end;
call ploop();

利用iterate 迭代循环,当然,iterate既可用于loop循环,也可用于while,repeate 这几种循环中,下面,以将iterate 用于loop循环为例:

create procedure piter()
begin
declare i int;
set i = 35;
loop_label:loop
if i = 43 then
set i = i+1;
iterate loop_label;
end if;
insert into t4 values(i);
set i = i+1;
if i>50 then
leave loop_label
end if;
end loop;
end;

call piter();


你可能感兴趣的:(数据库-MySQL)