关于oracle存储过程select into 未找到数据问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于oracle存储过程select into 未找到数据问题相关的知识,希望对你有一定的参考价值。

在存储过程中select xxx into 变量 from ... 如果未查到数据就会报未找到数据错误
一般处理方法就是exception when no_data_found then...(处理异常)

我想问的是该部分的执行过程,具体举例如下第一张图,
当没查到数据时,我的update语句会不会执行?
两张图的执行过程是否一样?
存储过程一般这样写好不好,或者该怎样写?
给些具体解释谢谢!

图1

图2

begin
 select count(*) into v_count from bpa_df_role where drename=record_drename and ddnid=ddnid_new;
 if v_count=0
  then
  select dreid into dreid_new from bpa_df_role where   drename=record_drename and   ddnid=ddnid_new;
   update....
else
   update...
end if;
end;

 

类似这样处理吧,得前边定义一个v_count的变量,百度这个换行换的真难看,能看懂吧?

不过update后必须要提交,要不重新进来,还是未更改状态

参考技术A 第一张图的update不会执行,因为select 。。into报错就直接到exception上了
第二张图可以执行update,但是如果select没有exception,你的update也就不会执行了。
exception有些象过程语言的goto语句,但它只在出现例外时才执行update,没有例外就不执行追问

那我两种情况都想执行update该怎么办

参考技术B 按我的理解,第一个图 不会执行到 update,第二个图会执行。

请教关于oracle中写存储过程时 select into 语句报错的问题

如果使用select into 语句取数据的话,假如没有符合条件的数据被取出,此时会报data not
found 错误,哪怎么样避免呢?通常的高效的做法是什么?是先使用select count(*)进行判断,还是通过使用类似于java里的异常或者类似的机制来解决呢?希望高手们指教,刚开始学习,

定义一个块,块中说明异常情况下如何处理就可以了。
begin
select 字段 into 变量...
exception
when NO_DATA_FOUND then --未找到时
处理逻辑
when exception_code then --这里的错误代码可以在standard包中找到
处理逻辑
when OTHERS then --default错误时
处理逻辑
end;
错误代码可通过如下语句获得:
select * from dba_source where owner='SYS' and name='STANDARD' and type='PACKAGE' and text like ' pragma EXCEPTION%';
参考技术A 最简单的语句:就是在你的字段加上max函数。如
select max(id)into var_id from tab where id=某一个值
呵呵,谁用谁明白。
参考技术B exception
when no_data_found then
--你的处理逻辑
参考技术C exception
when no_data_found then
null;

以上是关于关于oracle存储过程select into 未找到数据问题的主要内容,如果未能解决你的问题,请参考以下文章

请教关于oracle中写存储过程时 select into 语句报错的问题

oracle存储过程select语句必须带into吗

如何解决oracle存储过程select into问题

Oracle 存储过程学习

oracle 中SQL 语句开发语法 SELECT INTO含义

Oracle 未找到多个 Select Into 语句的数据