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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

勤快的懒羊羊   2020-09-29 我要评论

本文着重讲解了Oracle用户自定义异常实现过程解析,文中通过代码实例讲解的非常细致,对大家的工作和学习具有一定的参考学习价值,欢迎大家阅读和收藏

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

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

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

set serveroutput on
declare
no_result exception; --自定义异常
v_ssid student_test.sid%type;
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异常里,

if SQL%NOTFOUND then
  RAISE no_result;
end if;

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

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们