ORACLE 异常处理

一、

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

常用的异常类型有:

no_data_found:没有发现数据

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

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

二、例子

  1. -- v_code : 000 ,表示执行成功,其它表示执行失败  
  2. create or replace procedure detector_plsql_exception(  
  3.        v_deptno varchar2,  
  4.        v_dname out varchar2,  
  5.        v_code out varchar2,  
  6.        v_msg out varchar2  
  7. )  
  8. as  
  9.   
  10. begin   
  11.    select d.dname into v_dname from dept d where d.deptno = v_deptno;  
  12.    v_code := '000';  
  13.    exception  
  14.        when no_data_found then   
  15.           v_code := '001';  
  16.           v_msg := '找不到deptno为'||v_deptno||'的记录';  
  17.        when too_many_rows then  
  18.           v_code := '002';  
  19.           v_msg := 'deptno为'||v_deptno||'的记录多于一条';  
  20.        when others then  
  21.           v_code := '999';  
  22.           v_msg := '其它异常,'||sqlcode||','||sqlerrm;  
  23.           --sqlcode:当前错误代码  
  24.           --sqlerrm:当前错误消息文件  
  25. end detector_plsql_exception;  

原文地址:https://www.cnblogs.com/JSD1207ZX/p/9386340.html