注意:普通的查询语句不会出现异常,只有使用into对变量进行赋值的时候才会发生异常
1 --系统变量: notfound --> if sql%notfund then 如果这个表达式为真,则 (增删改)出错2 --,先自定义一个异常:no_result exception3 -- if sql%nofund then4 --excetpion5 --when no_result then 6 --dbms……
用户自定义异常写在:declare里,如:
1 set serveroutput on2 declare3 no_result exception; --自定义异常4 v_ssid student_test.sid%type;
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 then10 dbms_output.put_line('系统异常,违反主键约束');11 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异常。
联系客服