PLSQL跳出嵌套循环的小例子

开始

DECLARE
   v_cnt NUMBER:=0;
   v_out_ext BOOLEAN:=FALSE;
   v_in_ext  BOOLEAN:=FALSE;
BEGIN
   <<Outer_loop>>
   LOOP
         
     EXIT WHEN v_cnt>20;

     <<Inner_loop>>
     LOOP
         dbms_output.put_line(v_cnt);
         v_cnt:=v_cnt+1;
         
         IF v_cnt>5 THEN
             v_in_ext:=TRUE;
         END IF;
         
         EXIT Outer_loop WHEN (v_in_ext = TRUE);         
     END LOOP; 
     dbms_output.put_line('++++++++++++++++++++');
   END LOOP;
END;

运行结果:

anonymous block completed
0
1
2
3
4
5

如果改成跳出内部循环:

DECLARE
   v_cnt NUMBER:=0;
   v_out_ext BOOLEAN:=FALSE;
   v_in_ext  BOOLEAN:=FALSE;
BEGIN
   <<Outer_loop>>
   LOOP
         
     EXIT WHEN v_cnt>20;

     <<Inner_loop>>
     LOOP
         dbms_output.put_line(v_cnt);
         v_cnt:=v_cnt+1;
         
         IF v_cnt>5 THEN
             v_in_ext:=TRUE;
         END IF;
         
         EXIT Inner_loop WHEN (v_in_ext = TRUE);         
     END LOOP; 
     dbms_output.put_line('++++++++++++++++++++');
   END LOOP;
END;

运行结果:

anonymous block completed
0
1
2
3
4
5
++++++++++++++++++++
6
++++++++++++++++++++
7
++++++++++++++++++++
8
++++++++++++++++++++
9
++++++++++++++++++++
10
++++++++++++++++++++
11
++++++++++++++++++++
12
++++++++++++++++++++
13
++++++++++++++++++++
14
++++++++++++++++++++
15
++++++++++++++++++++
16
++++++++++++++++++++
17
++++++++++++++++++++
18
++++++++++++++++++++
19
++++++++++++++++++++
20
++++++++++++++++++++

结束

posted @ 2012-11-13 17:54  健哥的数据花园  阅读(5867)  评论(0编辑  收藏  举报