免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3925 | 回复: 2
打印 上一主题 下一主题

oracle 存储过程 异常问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-12-27 10:56 |只看该作者 |倒序浏览
oracle 9i 的存储过程,对一个select ...into...语句捕获异常。table 表中没有数据,举例如下:
   begin
     select
            a
     into
          v_a
     from table
     exception when others then
          v_a:= 0;
     end;     
可以捕获到异常,将v_a赋值为0;
但是 当选sum(a)的时候
   begin
     select
            sum(a)
     into
          v_a
     from table
     exception when others then
          v_a:= 0;
     end;   
就捕获不到异常,v_a的值为null。把others改为no_data_found也不行。
请问像这种 select sum()  这种语句,如果没有得到结果该如何捕获异常?还请指教,谢谢!

论坛徽章:
0
2 [报告]
发表于 2006-12-27 16:15 |只看该作者
原帖由 keybay oracle 9i 的存储过程,对一个select ...into...语句捕获异常。table 表中没有数据,举例如下:
   begin
     select
            a
     into
          v_a
     from table
     exception when others then
          v_a:= 0;
     end;     
可以捕获到异常,将v_a赋值为0;
但是 当选sum(a)的时候
   begin
     select
            sum(a)
     into
          v_a
     from table
     exception when others then
          v_a:= 0;
     end;   
就捕获不到异常,v_a的值为null。把others改为no_data_found也不行。


sum(x)会返回null,这也算是一个返回值,而对于第一种情况,则会触发no_data_found exception。所以如果在第二种情况中,可以自己加入一段condition,例如:
     declare
         Null_sum EXCEPTION;
     begin
     select
            sum(a)
     into
          v_a
     from table;

     if v_a is null then
        raise Null_sum;
     end if;

     exception
        when Null_sum then
           v_a:=0;
        when others then
           v_a:= 0;
        
     end;   

只是为了表述,具体功能要看你们自己的需求和系统设计。

论坛徽章:
0
3 [报告]
发表于 2006-12-29 11:31 |只看该作者
谢谢 numenhuang 的回答,如果select 有很多字段,就要一个一个的去判断,没有别的办法了么。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP