当在pl_sql中执行select hellip; into hellip;语句时,如果返回结果集为空,则回触发no_data_found错误。但是当 select 中有字
今天一朋友问及我这个问题,当记录不存在,,会提示 no data 的错误,下面是网上这类问题的解决方法。
当在pl_sql中执行select … into …语句时,如果返回结果集为空,则回触发no_data_found错误。但是当 select 中有字段用到函数时,即使结果集为空,也不会触发no_data_found错误。
create or replace procedure rd.exception_test is
test date;
begin
select end_time
into test
from rd.r_line_stop_time
where rownum = 1;
end exception_test;
当执行该存储过程时,会提示错误
ora-01403: no data found
ora-06512: at rd.exception_test, line 17
ora-06512: at line 1
create or replace procedure rd.exception_test is
test date;
begin
select max(end_time)
into test
from rd.r_line_stop_time
where rownum = 1;
end exception_test;
当执行带有函数的存储过程时,则并不会报no data found异常.
create or replace procedure rd.exception_test is
test varchar2(10);
begin
select to_char(end_time, 'yyyy-mm-dd')
into test
from rd.r_line_stop_time
where rownum = 1;
end exception_test;
但是在执行带to_char、substr等这样的函数会报no data found异常。
==========
我的理解是,当表中存在记录但字段值为空值,与表中不存在记录为空是两个不同的概念。一些集合函数如max,sum在处理的时候会自动引入null值。
不知sql server中是否也存在类似的问题?