ORACLE 异常处理
2014-05-02 01:39
162 查看
一、
开发PL/SQL程序时,需要考虑到程序运行时可能出现的各种异常,当异常出现时,或是中断程序运行,或是使程序从错误中恢复,从而继续运行。
常用的异常类型有:
no_data_found:没有发现数据
too_many_rows:select into 语句查询结果有多个数据行
others:可以捕捉所有异常,一般作为异常处理部分的最后一个异常处理器
二、例子
[sql] view
plaincopy
-- 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;
开发PL/SQL程序时,需要考虑到程序运行时可能出现的各种异常,当异常出现时,或是中断程序运行,或是使程序从错误中恢复,从而继续运行。
常用的异常类型有:
no_data_found:没有发现数据
too_many_rows:select into 语句查询结果有多个数据行
others:可以捕捉所有异常,一般作为异常处理部分的最后一个异常处理器
二、例子
[sql] view
plaincopy
-- 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;
相关文章推荐
- Oracle系列之异常处理
- ORACLE 异常处理问题处理
- Oracle PL/SQL 异常处理
- Oracle异常处理
- Oracle 异常处理
- Oracle 11g RAC环境下Private IP修改方法及异常处理
- Oracle 异常处理
- mybatis+oracle generator 配置和异常处理
- oracle 异常处理
- Oracle 异常处理
- oracle 异常处理问题
- Oracle的异常处理
- ORACLE学习之PL/SQL编程——异常处理
- 【初识Oracle】⑨异常处理EXCEPTION
- oracle的异常处理
- 浅析:oracle存储过程中的异常处理
- Oracle OGG 学习五: GoldenGate Exception Handler 异常处理二
- 《oracle pl/sql programming》 第六章 异常处理
- Oracle笔记 七、PL/SQL 异常处理
- Oracle存储过程中的异常处理