oracle中存储过程的异常处理

2021/4/19 19:29:49

本文主要是介绍oracle中存储过程的异常处理,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

oracle中存储过程的异常分为:
1.预定义异常:oracle已经定义了一个常量用于表示异常编号
异常 错误编号 常量名称
除数为0 -01476 ZERO_DIVIDE
案例:
create or replace procedure test_
as
c int;
begin
c:=10/0;
exception
when zero_divide then
dbms_output.put_line('除数不能为0哦');
end;

2.非预定义异常:错误编号没有对应的常量
create or replace procedure test_
as
c int;
my_error exception;
pragma exception_init(my_error,-01476);
begin
c:=10/0;
exception
when my_error then
dbms_output.put_line('除数不能为0哦');
when others then
dbms_output.put_line('未知错误');
end;
3.自定义异常
create or replace procedure move_moeny(
u1 int,
u2 int,
money_ dec
)
is
b dec(19,2); --读取u1余额
my_error exception; --定义自定义异常
begin
--判断转账的金额是否大于余额
select a.balance into b from account_ a where a.aid=u1;
if b<money_ then
raise my_error;
end if;

--如果余额大于转账金额,u1的余额要减少
update account_ set balance = balance-money_ where aid=u1;
--u2的余额要增加
update account_ set balance =balance+money_ where aid=u2;

--转账成功之后,要添加两条交易记录
insert into transfer values(x.nextval,sysdate,u1,'取出',money_);
insert into transfer values(x.nextval,sysdate,u2,'存入',money_);
exception
when my_error then
dbms_output.put_line('余额不足');
rollback;
end;
————————————————
版权声明:本文为CSDN博主「猫屎不是咖啡」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/heligui/article/details/78315856



这篇关于oracle中存储过程的异常处理的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程