当前位置: 代码迷 >> SQL >> (转)Oraclepl/sql中实现continue的步骤
  详细解决方案

(转)Oraclepl/sql中实现continue的步骤

热度:138   发布时间:2016-05-05 11:57:13.0
(转)Oraclepl/sql中实现continue的方法
转自:http://blog.csdn.net/wzy0623/article/details/1684344
Oracle pl/sql 中实现 continue
分类:?Oracle?3611人阅读?评论(0)?收藏?举报
oracleexceptionintegerinsertnulltable
CREATE?TABLE?AREAS
(
??RADIUS??
NUMBER(5),
??AREA????
NUMBER(14,2)
);

方法一:使用 goto

DECLARE
???
pi???CONSTANT?NUMBER?(9,?7)??:=?3.1415927;
???radius????????
INTEGER?(5);
???area??????????
NUMBER?(14,?2);
BEGIN
???radius?:
=?0;

???LOOP

??????
<<here>>
??????radius?:
=?radius?+?1;

??????
IF?radius?=?4
??????
THEN
?????????
GOTO?here;
??????
ELSE
?????????area?:
=?pi?*?POWER?(radius,?2);

?????????
INSERT?INTO?areas
??????????????
VALUES?(radius,?area);

?????????
EXIT?WHEN?area?>?100;
??????
END?IF;
???
END?LOOP;

???
COMMIT;
EXCEPTION
???
WHEN?OTHERS
???
THEN
??????
ROLLBACK;
??????RAISE;
END;

?-- 方法二:使用 exception

--?利用系统?exception
DECLARE
???pi?????CONSTANT?NUMBER?(
9,?7)??:=?3.1415927;
???radius??????????INTEGER?(
5);
???area????????????NUMBER?(
14,?2);
???some_variable???NUMBER?(
14,?2);
BEGIN
???radius?:
=?0;

???LOOP
??????BEGIN
?????????radius?:
=?radius?+?1;
?????????some_variable?:
=?1?/?(radius?-?4);
?????????area?:
=?pi?*?POWER?(radius,?2);

?????????INSERT?INTO?areas
??????????????VALUES?(radius,?area);

?????????EXIT?WHEN?area?
>?100;
??????EXCEPTION
?????????WHEN?ZERO_DIVIDE
?????????THEN
????????????NULL;
??????END;
???END?LOOP;

???COMMIT;
EXCEPTION
???WHEN?OTHERS
???THEN
??????ROLLBACK;
??????RAISE;
END;

?

--?自定义?exception
DECLARE
???
pi??????????????CONSTANT?NUMBER?(9,?7)??:=?3.1415927;
???radius???????????????????
INTEGER?(5);
???area?????????????????????
NUMBER?(14,?2);
???e_userdefinedexception???EXCEPTION;
???PRAGMA?EXCEPTION_INIT?(e_userdefinedexception,?
-1401);
BEGIN
???radius?:
=?0;

???LOOP
??????radius?:
=?radius?+?1;

??????
BEGIN
?????????
IF?radius?=?4
?????????
THEN
????????????RAISE?e_userdefinedexception;
?????????
ELSE
????????????area?:
=?pi?*?POWER?(radius,?2);

????????????
INSERT?INTO?areas
?????????????????
VALUES?(radius,?area);

????????????
EXIT?WHEN?area?>?100;
?????????
END?IF;
??????EXCEPTION
?????????
WHEN?e_userdefinedexception
?????????
THEN
????????????
NULL;
??????
END;
???
END?LOOP;

???
COMMIT;
EXCEPTION
???
WHEN?OTHERS
???
THEN
??????
ROLLBACK;
??????RAISE;
END;
转自:http://www.itpub.net/thread-1118901-1-1.html
如果CONTINUE只有一两处,而且可以轻易变成IF..ELSE.., 那么就用IF ELSE, 这也是TOM推荐的方法。
但有时候CONTINUE藏在其他复杂的控制结构里面,IF ELSE 就不容易搞定了。我会用这种方法:


FOR 或者 WHILE 主循环 LOOP
? ? FOR v_foo IN 1..1 LOOP? ???---- 第二层假循环

? ?? ???……代码……
? ?? ???IF …… THEN
? ?? ?? ???EXIT;? ?? ?---- 退出假循环,等效于CONTINUE主循环
? ?? ???END IF;
? ?? ???……代码……
? ?? ??
? ?? ???IF …… THEN?
? ?? ?? ???EXIT main_loop;? ? ---- 如果需要退出主循环,用这个写法
? ?? ???END IF;

? ?? ???……代码……

? ? END LOOP v_foo;??---- 第二层假循环结束
END LOOP main_loop; ---- 主循环结束

初看有些古怪,因为用EXIT代替了CONTINUE. 但习惯了就会发现很灵活。
  相关解决方案