oracle存储过程中ORA-01403: 未找到数据 问题解决方法

原文链接:https://blog.csdn.net/hp961218/article/details/80002256
错误提示:ORA-01403: 未找到数据

产生原因:当查询不到数据时,且把查询结果注入到定义的变量里面

解决方法:

            1.跳出此次存储过程: 可以写个错误提示:

                 when not_data_found then --后面一般接错误的数据提示消息

            2.不跳出存储过程: 

                由于错误的根本原因是数据不存在,导致的空值问题,所以可以根据问题的原理得到对应的解决方法。设置一个变量tempSize,类型为number,每次在查询注入前,执行一次 select count(1) into tempSize from table,然后判断tempSize是否大于1,如果大于1再执行查询赋值给其他变量,这样子就一定不会报找不到数据错误,因为count(1)就算找不到 值为0 ,可以赋值给tempSize,这样子就避免了空值问题.

分析:俩种方法都能解决空值问题,不过第二张方法更好,不用跳出存储过程,实际上也是,因为存储过程大多数是用于更新数据用的,不能因为一个数据找不到,导致大量数据无法更新,所以推荐使用第二种方法解决

对方法二的解释:

如:需要查询student表的sid,并赋给一个变量,之后进行一系列操作

select s.sid into studentId from student s where 条件;

这条语句没有找到指定的数据,导致在into的时候报错。可以将上述语句改成:

select count(*) into e_count from student s where 条件;
if e_count > 0 then
  select s.sid into studentId from student s;
end if;
拿到studentId后需要进行的操作




猜你喜欢

转载自blog.csdn.net/u010999809/article/details/80663895
今日推荐