ora-01403:未找到任何数据

来源:互联网 发布:天堂js手机版下载 编辑:程序博客网 时间:2024/05/22 12:19

需求:

打算从fa_product_net_value表中拿出一只基金对应的所有估值日期,再应用此日期计算相应的指标。


declare
v_date_1 date ;
v_product nvarchar2(20) := '00010000388015';
v_date date :=date'2015-06-19';
begin
DBMS_OUTPUT.ENABLE (buffer_size=>null);
 
while v_date<=date'2015-12-31'  loop
 
select t.d_esti_date into v_date_1 from dc_edw.fa_product_net_value t
where t.sk_product = v_product
and t.d_esti_date = v_date ;

dbms_output.put_line(v_date_1);
v_date := v_date+1 ;
end loop ;

end;


运行时 报错


网上找解决方案: 提示说是因为fa_product_net_value表中没有查出数据,(当表中存在记录但字段值为空值,与表中不存在记录为空是两个不同的概念),查找表发现

2015年7月25日没有数据

select * from dc_edw.fa_product_net_value t
where t.sk_product = '00010000388015' ;


解决办法:

捕获异常,用exception when no_data_found then  处理

declare
v_date_1 date ;
v_product nvarchar2(20) := '00010000388015';
v_date date :=date'2015-06-19';
begin
DBMS_OUTPUT.ENABLE (buffer_size=>null);
 
while v_date<=date'2015-12-31'  loop
 
select t.d_esti_date into v_date_1 from dc_edw.fa_product_net_value t
where t.sk_product = v_product
and t.d_esti_date = v_date ;

dbms_output.put_line(v_date_1);
v_date := v_date+1 ;
end loop ;
exception when no_data_found then
  v_date_1 := date'9999-01-01';
end;

能正常运行,但是发现,异常抛出后,无法继续运行while程序。

结果只显示到2015-07-24日。

分析原因: 当程序跑到2015-07-25的时候,没有数据,异常抛出给定的默认值‘9999-01-01’,程序结束。

尝试使用聚合函数。

declare
v_date_1 date ;
v_product nvarchar2(20) := '00010000388015';
v_date date :=date'2015-06-19';
begin
--DBMS_OUTPUT.ENABLE (buffer_size=>null);
while v_date<=date'2015-12-31'  loop
select max(t.d_esti_date) into v_date_1 from dc_edw.fa_product_net_value t
where t.sk_product = v_product
and t.d_esti_date = v_date ;
dbms_output.put_line(v_date_1);
v_date := v_date+1 ;
end loop ;
end;

完美解决问题。

分析原因:执行带有函数的存储过程时,则并不会报no data found异常.


0 0