服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Oracle - Oracle用户自定义异常实现过程解析

Oracle用户自定义异常实现过程解析

2020-12-19 21:51勤快的懒羊羊 Oracle

这篇文章主要介绍了Oracle用户自定义异常实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

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

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

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

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

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

?
1
2
3
if SQL%NOTFOUND then
  RAISE no_result;
end if;

SQL%NOTFOUND是检查更新语句是否更新成功,如果更新失败,则notfound语句为真,
则使用raise语句跳转到no_result异常执行。

(dup_val_on_index)异常是系统异常,如果使用插入语句并且违反主键唯一性约束,则执行dup_val_on_index异常。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.cnblogs.com/lwl80/p/13253853.html

延伸 · 阅读

精彩推荐