PL/SQL异常

一.异常
在oracle中的异常分为3种异常,第一种是预定义异常,这种异常是oracle已经定义好名称的异常,也就是异常名称和异常编码是对应好的。例如:NO_DATE_FOUND 错误编码为+100。第二种就是非预定义异常:这种异常就是有oracle已经给定了错误编码和错误描述,所以不会被触发。当声明一个异常名字并且关联到异常编码,当有异常时就能自动触发。第三种就是自定义异常,需要自己决定异常触发的条件,并且显示触发即用代码调用异常。

--1.非预定义异常
declare 
  var_id number(4);
  test_exception EXCEPTION;
  pragma exception_init(test_exception,-00001);
begin
  var_id:=11;
  update emp e set e.empno = var_id where e.empno = 10;
  exception
    when test_exception then
      dbms_output.put_line(SQLERRM);
      rollback;
end;

--2.自定义异常
declare 
  var_id number(4);
  test_exception exception;
  pragma exception_init(test_exception,-2000);

begin
  select e.empno into var_id from emp e where e.mgr = 7902;
  if var_id > 100 then
    raise test_exception;
   end if;
   dbms_output.put_line('===========');
  exception
    when test_exception then
      dbms_output.put_line('大于10');
    when TOO_MANY_ROWS then
      dbms_output.put_line('too many');
end;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值