pl/sql exception例子

--exception异常
declare
v_sal employees.salary%type;
begin 
select salary into v_sal from employees where employee_id=100;
exception
--非预定义异常
when too_many_rows then 
dbms_output.put_line('不只一条数据');


when others then 
dbms_output.put_line('出现其它异常');
end;
set serveroutput on;
--pragna的使用,将自定义异常与预定义异常相关联
declare 
delete_exception exception;
pragma exception_init(delete_exception ,-2292);
begin 
delete from employees where employee_id=100;
Exception WHEN delete_exception then 
dbms_output.put_line('非完整性异常');
end;
--raise的使用,若工资高于6000抛出自定义异常
declare
v_sal employees.salary%type;
v_high_sal exception;
begin 
select salary into v_sal from employees where employee_id=100;
if v_sal >6000 then raise v_high_sal;
end if;
exception
when v_high_sal THEN dbms_output.put_line('工资过高');
end;


set serveroutput on;
--抛出预定义异常,no_data_found,查询员工 ,若没有该员工,则抛出异常
declare 
v_sal employees.salary %type;
begin
select salary into v_sal from employees where employee_id=1000;
exception
when no_data_found then dbms_output.put_line('未找到相应数据');

end;


--自定义异常,隐式游标
declare
no_result exception;
begin 
update employees1 set salary=salary+100 where employee_id=1000;
if sql%notfound then 
raise no_result;
end if;
exception 
when no_result then 
dbms_output.put_line('未找到该员工 ');
end;

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值