数据库MySQL-循环结构
分类:
while、loop、repeat
循环控制:
iterate类似于 continue,继续结束本次循环,继续下一次
leave 类似于break,跳出,结束当前所在的循环
*/
语法:
【标签:】while 循环条件 do
循环体;
end while【标签】;
*/
语法:
【标签:】loop
循环体;
end loop 【标签】;
可以用来模拟简单的死循环
*/
语法:
【标签:】repeat
循环体;
until 结束循环的条件;
end repeat 【标签】;
*/
CREATE PROCEDURE pro_while(IN insertCount INT)
BEGIN
DECLARE i INT DEFAULT 1;
WHILE i <= insertCount DO
INSERT INTO admin(username,PASSWORD) VALUE(CONCAT('Rose',i),'666');
SET i = i + 1;
END WHILE;
END$
CALL pro_while(100)$
TRUNCATE TABLE admin$
CREATE PROCEDURE test_while1(IN insertCount INT)
BEGIN
DECLARE i INT DEFAULT 1;
a:WHILE i <= insertCount DO
INSERT INTO admin(username,PASSWORD) VALUES(CONCAT('xiaohua',i),'0000');
IF i >= 20 THEN LEAVE a;
END IF;
SET i = i + 1;
END WHILE a;
END$
CALL test_while1(100)$
TRUNCATE TABLE admin$
CREATE PROCEDURE test_while2(IN insertCount INT)
BEGIN
DECLARE i INT DEFAULT 0;
a:WHILE i <= insertCount DO
SET i = i + 1;
IF MOD(i,2) = 1 THEN ITERATE a;
END IF;
INSERT INTO admin(username,PASSWORD) VALUES(CONCAT('xiaohua',i),'0000');
END WHILE a;
END$
CALL test_while2(100)$