ORACLE 异常处理

一、

开发PL/SQL程序时,需要考虑到程序运行时可能出现的各种异常,当异常出现时,或是中断程序运行,或是使程序从错误中恢复,从而继续运行。

常用的异常类型有:

no_data_found:没有发现数据

too_many_rows:select into 语句查询结果有多个数据行

others:可以捕捉所有异常,一般作为异常处理部分的最后一个异常处理器

二、例子

-- v_code : 000 ,表示执行成功,其它表示执行失败
create or replace procedure detector_plsql_exception(
       v_deptno varchar2,
       v_dname out varchar2,
       v_code out varchar2,
       v_msg out varchar2
)
as

begin 
   select d.dname into v_dname from dept d where d.deptno = v_deptno;
   v_code := '000';
   exception
       when no_data_found then 
          v_code := '001';
          v_msg := '找不到deptno为'||v_deptno||'的记录';
       when too_many_rows then
          v_code := '002';
          v_msg := 'deptno为'||v_deptno||'的记录多于一条';
       when others then
          v_code := '999';
          v_msg := '其它异常,'||sqlcode||','||sqlerrm;
          --sqlcode:当前错误代码
          --sqlerrm:当前错误消息文件
end detector_plsql_exception;

..

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值