好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

查看更多关于Oracle用户自定义异常实现过程解析的详细内容...

  阅读:21次