oracle存储过程调用中的异常

1.问题描述

oracle存储过程在相互调用(外层调用内层)时,如果内层存储过程出现异常,则内层抛异常语句后面的语句不会继续执行,外层也会抛出异常且后面的语句也不会执行。

如果内层存储过程对异常进行了捕获,外层就不会抛出异常并正常执行。

2.测试案例

内层存储过程:

create or replace procedure exception_test2 is

    v_result number;

    o_result_info varchar2(2000);

begin 

    select 1/0 into v_result from dual;

    dbms_output.put_line('hello world!');

    exception

    when others then rollback;

    o_result_info:='处理失败'||sqlerrm(sqlcode);

    commit; 

    dbms_output.put_line(o_result_info);

end exception_test2;

输出结果:处理失败ORA-01476: 除数为 0


外层存储过程:

create or replace procedure exception_test1 is

  v_error_info varchar2(2000);

  cursor test_cursor is select 1 from dual;                  --创建游标

  var_num number;

begin

  open test_cursor;                                                    --打开游标

  fetch test_cursor into var_num;                              --使用游标

  exception_test2;                                                      --调用内层存储过程

  close test_cursor;                                                   --关闭游标

  dbms_output.put_line('hello world!');

  exception                                                                --捕获异常

    when others then rollback;

    v_error_info:='处理失败'||sqlerrm(sqlcode);

    commit;

    dbms_output.put_line(v_error_info);                   --输出异常信息

end exception_test1;

输出结果:1

处理失败ORA-01476: 除数为 0

hello world!

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容