我是靠谱客的博主 危机野狼,这篇文章主要介绍Oracle用户自定义异常,现在分享给大家,希望可以做个参考。

注意:普通的查询语句不会出现异常,只有使用into对变量进行赋值的时候才会发生异常

复制代码
1
2
3
4
5
6
1 --系统变量: notfound --> if sql%notfund then 如果这个表达式为真,则 (增删改)出错 2 --,先自定义一个异常:no_result exception 3 -- if sql%nofund then 4 --excetpion 5 --when no_result then 6 --dbms……

用户自定义异常写在:declare里,如:

复制代码
1
2
3
4
1 set serveroutput on 2 declare 3 no_result exception; --自定义异常 4 v_ssid student_test.sid%type;
复制代码
1
2
3
4
5
6
7
8
9
10
11
1 begin 2 update student_test set sex='男' where sid=1000002; --没有异常,报(自定义异常)插入为空的错误 3 if SQL%NOTFOUND then 4 RAISE no_result; 5 end if; 6 exception 7 when no_result then 8 dbms_output.put_line('修改有误!'); 9 when dup_val_on_index then 10 dbms_output.put_line('系统异常,违反主键约束'); 11 end;

如果修改语句修改为空,系统不会报错,但会直接进入用户自己定义的no_result异常里,

复制代码
1
2
3
if SQL%NOTFOUND then RAISE no_result; end if;
复制代码
1
2
3
4
5
SQL%NOTFOUND是检查更新语句是否更新成功,如果更新失败,则notfound语句为真, 则使用raise语句跳转到no_result异常执行。 (dup_val_on_index)异常是系统异常,如果使用插入语句并且违反主键唯一性约束, 则执行dup_val_on_index异常。

最后

以上就是危机野狼最近收集整理的关于Oracle用户自定义异常的全部内容,更多相关Oracle用户自定义异常内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(51)

评论列表共有 0 条评论

立即
投稿
返回
顶部