避免NO_DATA_FOUND

目前仅知道select   into 会触发no_data_found异常,原因是将空值赋值给变量,有点像java中空指针异常。

解决方法:

1、将查询语句结果赋予游标,通过sql%notfound检验

2、先select  count(*)  into v_count 通过判断v_count来检查是否查找到结果(update、delete 可以通过sql%rowcount(隐式游标)判断是否有更新)

例:group by ---->no_data_found

declare
  v_count int;
 begin
    select employee_id into v_count from employees where employee_id=11 group by employee_id;
   exception
     when no_data_found then
       dbms_output.put_line('no_data_found');
 end;

此例将会产生no_data_found异常,原因为group by的执行顺序即oracle中先统计count,然后在group by ,如果count=0,再group by 将返回空指针,导致no_data_found异常

使用下例进行规避:

declare
  v_count int;
 begin
    select count(*) into v_count from (select employee_id  from employees where employee_id=11 group by employee_id);
    dbms_output.put_line(v_count);
    exception
     when no_data_found then
       dbms_output.put_line('no_data_found');
 end;


总结:
1.一定要注意在使用group by时,SELECT COUNT(*) INTO语句也会产生NO_DATA_FOUND 异常。 
2.一定要理解好group by执行的顺序。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值