mysql存储过程循环 while/repeat/loop

#mysql存储过程循环 while/repeat/loop
##1.while 条件 do … end while

mysql> create procedure proce_while()
    -> begin
    -> declare count int;
    -> set count = 0;
    -> while count < 5 do
    -> insert into onecolumn values(count);
    -> set count = count + 1;
    -> end while;
    -> end//

##2.repeat xxx until 条件 end repeat

mysql> create procedure proce_repeat()
    -> begin
    -> declare
    -> count int;
    -> set count = 10;
    -> repeat
    -> insert into onecolumn values(count);
    -> set count = count + 1;
    -> until count >=15 end repeat;
    -> end
    -> //
Query OK, 0 rows affected (0.00 sec)

##3.loop_label:loop … leave loop_lable … end loop

> create procedure proce_loop()
    -> begin
    -> declare count int;
    -> set count = 20;
    -> loop_lable:loop
    -> insert into onecolumn values(count);
    -> set count = count + 1;
    -> if(count >=25) then 
    -> leave loop_lable;
    -> end if;
    -> end loop;
    -> end//

你可能感兴趣的:(mysql,数据库)