MySql 的三大循环 (while ,repeat,loop)
while 循环示例=============================================
delimiter $$
CREATE PROCEDURE `sp_while`()
COMMENT ‘循环示例‘
begin
declare var int ;
set var=1;
while var<=10000 do
insert into te values(var);
set var=var+1;
end while ;
end;
$$ delimiter ;
repeat循环示例===========================================
delimiter $$
CREATE PROCEDURE `sp_repeat`()
COMMENT ‘循环示例‘
begin
declare var int ;
set var=1;
repeat
insert into te values(var);
set var=var+1;
until var >10000
end repeat;
end;
$$ delimiter ;
loop 循环示例==========================================
delimiter $$
CREATE PROCEDURE `sp_loop`()
COMMENT ‘循环示例‘
begin
declare var int ;
truncate table te;
set var=1;
lp1:loop
if var>10000 then
leave lp1;
end if ;
insert into te values(var);
set var=var+1;
end loop;
end;
$$ delimiter;
本文出自 “SQLServer MySQL” 博客,请务必保留此出处http://dwchaoyue.blog.51cto.com/2826417/1359276