在存储过程中定义了一个变量: v_monthflag
select month_id from table1 where month_id = substr(vi_sdate,1,6)
选出的结果为空
当使用:
select month_id into v_monthflag from adjust_settle_hlxk_ehome_diff
where month_id = substr(vi_sdate,1,6) 时程序跑不过去,报错,选不出来值。
然而:
select (select month_id from table1 where month_id = substr(vi_sdate,1,6) )
into v_monthflag from dual;
可以正常通过
这个如何理解!!!
感谢网友解释。
1、对于第一个语句,对于select ...into,如果不会返回任何数据行,按oracle规定会触发no_data_found异常。
2、第二个语句,括号中的select语句为标量查询,标量查询对于返回一个及1个以下的值(NULL)认为是正确,不会存在逻辑错误,并且外层select是不带条件的dual查询,会返回一行数据。因此返回一个空值,而不会报错。
测试代码:
declare
DNAMEflag varchar2(14);
begin
select DNAME into DNAMEflag from dept
where DNAME = ' SEARCH';
dbms_output.put_line(DNAMEflag);
exception
when no_data_found then
dbms_output.put_line('no data');
end;