Oracle基本操作十:异常处理

--异常处理
--规则:
   1.exception关键字
   2.PL/SQL异常没有任何父子关系
   3.when others为最后一条子句,捕获所有未处理的异常
   4.可设置多个异常代理代码
   5.异常快一次运行,只有一个异常处理器处理异常
--种类:
   1.预定义异常--有异常名,错误代码,异常信息,24种,如ORA-0001
   2.非预定义异常--无异常名,错误代码,异常信息
   3.自定义异常--少用
--举例
declare
  r_tb_clazz tb_clazz %rowtype;
begin
  select * 
  into r_tb_clazz
  from tb_clazz
  where id=1;
exception
  when no_data_fount then
    dbms_output.putline('数据未找到');--若id=1未找到数据
  when others then
    dbms_output.putline('others'); 
end;


--两个异常相关的函数:
 --sqlcode:错误代码
  --sqlerrm:错误信息
  用法:--把异常信息存到表中
  1.创建异常表
  create table tb_error(
  id number primary key,
  tablename varchar2(18),
  sqlcode varchar2(50),
  sqlerrm varchar2(200),
  currdate date default sysdate
  )
  2.创建sequence
  create sequence seq_tb_error;
  3.--使用
  --举例
declare
  r_tb_clazz tb_clazz %rowtype;
  v_sqlcode varchar2(50),
  v_sqlerrm varchar2(200),
begin
  select * 
  into r_tb_clazz
  from tb_clazz
  where id=1;
exception
  when no_data_fount then
    v_sqlcode:=sqlcode;
v_sqlerrm:=sqlerrm;
insert into tb_error(id,table,sqlcode,sqlerrm)
values(seq_tb_error,'tablename',v_sqlcode,v_sqlerrm);
  when others then
    dbms_output.putline('others'); 
end;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值