SQL开发知识: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异常。

注意:普通的查询语句不会出现异常,只有使用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异常。

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

我想要获取技术服务或软件
服务范围:MySQL、ORACLE、SQLSERVER、MongoDB、PostgreSQL 、程序问题
服务方式:远程服务、电话支持、现场服务,沟通指定方式服务
技术标签:数据恢复、安装配置、数据迁移、集群容灾、异常处理、其它问题
沟通购买:QQ咨询 淘宝咨询 微信咨询 淘宝店铺
版权申明及联系
本站部分文章参考或来源于网络,如有侵权请联系站长。本站提供相关远程技术服务,有需要可联系QQ
数据库远程运维 » SQL开发知识:Oracle用户自定义异常实现过程解析