delimiter // #定义标识符为双斜杠
drop procedureifexiststest; #如果存在test存储过程则删除
createproceduretest() #创建无参存储过程,名称为testbegindeclareiint; #申明变量
set i = 0; #变量赋值
while i < 10do #结束循环的条件: 当i大于10时跳出while循环
insert into test values (i); #往test表添加数据
set i = i + 1; #循环一次,i加一
endwhile; #结束while循环
select * from test; #查看test表数据
end// #结束定义语句
call test(); #调用存储过程
二、repeat循环
delimiter // #定义标识符为双斜杠
drop procedureifexiststest; #如果存在test存储过程则删除
createproceduretest() #创建无参存储过程,名称为testbegindeclareiint; #申明变量
set i = 0; #变量赋值
repeat
insert into test values (i); #往test表添加数据
set i = i + 1; #循环一次,i加一
until i > 10endrepeat; #结束循环的条件: 当i大于10时跳出repeat循环
select * from test; #查看test表数据
end// #结束定义语句
call test(); #调用存储过程
三、loop循环
delimiter // #定义标识符为双斜杠
drop procedureifexiststest; #如果存在test存储过程则删除
createproceduretest() #创建无参存储过程,名称为testbegindeclareiint; #申明变量
set i = 0; #变量赋值
lp : loop #lp为循环体名,可随意 loop为关键字
insert into test values (i); #往test表添加数据
set i = i + 1; #循环一次,i加一
if i > 10then #结束循环的条件: 当i大于10时跳出loop循环
leave lp;
endif;
endloop;
select * from test; #查看test表数据
end// #结束定义语句
call test(); #调用存储过程