循环原则:
如果循环内部必须执行一次,则使用基本循环
如果知道循环次数,则使用FOR循环
如果必须在每次循环开始时判断条件,则使用WHILE循环
1、基本循环
SQL> declare
2 i number := 0;
3 begin
4 loop
5 dbms_output.put_line(i);
6 i := i+1;
7 exit when i = 10;
8 end loop;
9 end;
10 /
0
1
2
3
4
5
6
7
8
9
PL/SQL procedure successfully completed.
SQL> declare
2 i number := 0;
3 begin
4 loop
5 i := i+1;
6 dbms_output.put_line(i);
7 exit when i = 10;
8 end loop;
9 end;
10 /
1
2
3
4
5
6
7
8
9
10
PL/SQL procedure successfully completed.
2、FOR循环
SQL> declare
2 i number := 0 ;
3 begin
4 for i in 0 .. 10 loop
5 dbms_output.put_line(i);
6 end loop;
7 end;
8 /
0
1
2
3
4
5
6
7
8
9
10
PL/SQL procedure successfully completed.
3、WHILE循环
SQL> declare
2 i number:=0;
3 begin
4 while i<=10 loop
5 dbms_output.put_line(i);
6 i:=i+1;
7 end loop;
8 end;
9 /
0
1
2
3
4
5
6
7
8
9
10
PL/SQL procedure successfully completed.