您的位置:首页 > 数据库 > Oracle

Oracle中no_data_found异常处理:替代count(*)>0的前期判断

2020-06-08 04:50 211 查看

遍历某个表中的数据,或通过select into 给某个变量赋值时,下面这种情况会抛出no_data_found异常,以前我会用select count(pid) into countPerson进行前期判断,判断countPerson 是否大于0,这样不仅增加了代码的冗余,还是代码可读性变差。
通过下面处理让代码变得清爽:begin exception then end;(存储过程等中可以出现多个begin,end;也可以相互嵌套)
Begin
–可能出现异常
SELECT pid INTO temp_pid FROM t_person WHERE ID_CARD_NUMBER=‘1112233xxxxxxxx2323’;
Exception when no_data_found then
–处理逻辑
DBMS.OUTPUT_PUTLINE(“没有找到该身份证对应的用户!”);
when others then raise;
End;

循环中的使用方法:
Begin
–可能出现异常
for rec_person_id in(select pid from t_person)loop
–处理逻辑(可能出现no_data_found异常)
end loop;
Exception when no_data_found then
–处理逻辑
DBMS.OUTPUT_PUTLINE(“没有数据”);
End;

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐