oracle 跳出内层循环,内层程序中发生异常后,不会继续执行外层程序的语句
開發寫了個存儲過程需要我們審批,發現子程序中使用了異常處理語句,
通過以下實驗說明這種寫法的問題:
SQL> create table test_number(test_id number);
Table created
Executed in 0.031 seconds
不使用子程序,正常執行:
SQL> declare
2 ? ?v_err_mess varchar2(200);
3 ? ?n_count number;
4 ?begin
5 ? ?select count(1) into n_count from test_number;
6 ? ?dbms_output.put_line(n_count);
7 ?exception
8 ? ?when others then
9 ? ? ?v_err_mess:=sqlcode||' '||sqlerrm;
10 ? ? ?dbms_output.put_line(v_err_mess);
11 ?end;
12 ?/
0
PL/SQL procedure successfully completed
不使用子程序,異常執行:
SQL> declare
2 ? ?v_err_mess varchar2(200);
3 ? ?n_count number;
4 ?begin
5 ? ?select test_id into n_count from test_number;
6 ? ?dbms_output.put_line(n_count);
7 ?exception
8 ? ?when others then
9 ? ? ?v_err_mess:=sqlcode||' '||sqlerrm;
10 ? ? ?dbms_output.put_line(v_err_mess);
11 ?end;
12 ?/
100 ORA-01403: 未找到任何數據
PL/SQL procedure successfully completed
使用子程序,在子程序中進行異常處理:
可以看到,在外層調用中無法獲取子程序異常處理中的賦值
SQL>
SQL> declare
2 ? ?v_err_mess varchar2(200);
3 ? ?n_count number;
4 ?begin
5 ? ?begin
6 ? ? ?select test_id into n_count from test_number;
7 ? ? ?dbms_output.put_line(n_count);
8 ? ?exception
9 ? ? ?when others then
10 ? ? ? ?v_err_mess:=sqlcode||' '||sqlerrm;
11 ? ? ? ?dbms_output.put_line(v_err_mess);
12 ? ? end;
13
14 ? ? dbms_output.put_line('inner layer values:'||n_count);
15 ?exception
16 ? ?when others then
17 ? ? ?v_err_mess:='out_layer:'||sqlerrm;
18 ? ? ?dbms_output.put_line(v_err_mess);
19 ?end;
20 ?/
100 ORA-01403: 未找到任何數據
inner layer values:
PL/SQL procedure successfully completed
使用子程序,在子程序中進行正常處理:
可以看到,在外層調用中可以獲取子程序中的賦值
SQL> declare
2 ? ?v_err_mess varchar2(200);
3 ? ?n_count number;
4 ?begin
5 ? ?begin
6 ? ? ?select count(1) into n_count from test_number;
7 ? ? ?dbms_output.put_line(n_count);
8 ? ?exception
9 ? ? ?when others then
10 ? ? ? ?v_err_mess:=sqlcode||' '||sqlerrm;
11 ? ? ? ?dbms_output.put_line(v_err_mess);
12 ? ? end;
13
14 ? ? dbms_output.put_line('inner layer values:'||n_count);
15 ?exception
16 ? ?when others then
17 ? ? ?v_err_mess:='out_layer:'||sqlerrm;
18 ? ? ?dbms_output.put_line(v_err_mess);
19 ?end;
20 ?/
0
inner layer values:0
PL/SQL procedure successfully completed
以上實驗
內層程序中發生異常后,不會繼續執行外層程序的語句。
可以在最外層進行異常處理。
來自 “ ITPUB博客 ” ,鏈接:http://blog.itpub.net/26451536/viewspace-1815947/,如需轉載,請注明出處,否則將追究法律責任。
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的oracle 跳出内层循环,内层程序中发生异常后,不会继续执行外层程序的语句的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么查看oracle数据库表的主键,Or
- 下一篇: linux通过ftp自动上传文件到服务器