MySQLストアドプロシージャWHILE、REPEAT、LOOPの使い方

1.while
mysql> create procedure prodemo10()
-> begin
-> declare i int;
-> set i=0;
-> while i<8 do
-> insert into t1(filed) values(i);
-> set i=i+1;
-> end while;
-> end;
mysql> delete from t1
mysql> call prodemo10()
mysql> select * from t1

2.REPEAT……END REPEAT
mysql> create procedure prodemo11()
-> begin
-> declare i int default 0;
-> repeat
-> insert into t1(filed) values(i);
-> set i=i+1;
-> until i>=8
-> end repeat;
-> end;
mysql> delete from t1
mysql> call prodemo11()

3.LOOP……END LOOP
mysql> create procedure prodemo12()
-> begin
-> declare i int default 0;
-> loop_label: loop
-> insert into t1(filed) values(i);
-> set i=i+1;
-> if i>=8 then
-> leave loop_label;
-> end if;
-> end loop;
-> end;
mysql> delete from t1
mysql> call prodemo12
mysql> select * from t1

4.Labels…END Labels
mysql> create procedure prodemo13()
-> label_1:begin
-> label_2:while 0=1 do leave label_2;end while;
-> label_3:repeat leave label_3;until 0=0 end repeat;
-> label_4:loop leave label_4;end loop;
-> end;
mysql> create procedure pro14()
-> label_1:begin
-> label_2:while 0=1 do leave label_2;end while label_2;
-> label_3:repeat leave label_3;until 0=0 end repeat label_3;
-> label_4:loop leave label_4;end loop label_4;
-> end label_1;
5.iterate
mysql> create procedure prodemo15()
-> begin
-> declare i int default 0;
-> loop_label:loop
-> if i=3 then
-> set i=i+1;
-> iterate loop_label;
-> end if;
-> insert into t1(filed) values(i);
-> set i=i+1;
-> if i>=8 then
-> leave loop_label;
-> end if;
-> end loop;
-> end;
mysql> delete from t1
mysql> call prodemo15
mysql> select * from t1

Comments are closed.