1、使用while
DROP PROCEDURE IF EXISTS `addstudent`;
DELIMITER ;;
CREATE PROCEDURE `addstudent`(iNum int)
BEGIN
declare vI int default 0;
start transaction;
while(vI < iNum) do
insert into student(id,name) values(vI,concat(‘Andy‘,vI));
set vI = vI+1;
end while;
commit;
END
;;
DELIMITER ;
2、使用repeat
BEGIN
declare vI int default 0;
start transaction;
repeat
insert into student(id,name) values(vI,concat(‘Andy‘,vI));
set vI = vI+1;
until vI >= iNum end repeat;
commit;
END
3、使用loop
BEGIN
declare vI int default 0;
start transaction;
label_insert: LOOP
insert into student(id,name) values(vi,concat(‘Andy‘,vi));
set vI = vI+1;
if(vI >= iNum) then
leave label_insert;
end if;
end LOOP label_insert;
commit;
END
4、考虑下面的需求,在编程语言中循环控制中有continue,如何实现continue的功能?
使用 iterate label_insert; 对于LOOP如下:
BEGIN
declare vI int default 0;
start transaction;
label_insert: LOOP
if(vI=2) then
set vI = vI+1;
iterate label_insert;
end if;
insert into student(id,name) values(vi,concat(‘Andy‘,vi));
set vI = vI+1;
if(vI >= iNum) then
leave label_insert;
end if;
end LOOP label_insert;
commit;
END
注意:对于while和repeat也是同样的道理,但是需要在while和repeat 前面增加一个标签,表示再次循环的位置,如下:
label_insert: while
label_insert: repeat
mysql中暂时没有for循环。
原文:http://www.cnblogs.com/nzbbody/p/4604609.html