我在存储过程里需要用到loop循环,循环中如果碰到查询不到数据时,继续循环.像下面这样写的loopselect nvl(mtrl_id,0) into v_mtrl_id
from table
where mtrl_id............;end loop;查询空行时,报错,"找不到数据".
如果这样写的话
loop
select count(*) into v_count from table where .............;
if count(*) >0 then
      select nvl(mtrl_id,0) into v_mtrl_id
      from table
      where ............;
end if;end loop;查询速度本来就不快,速度慢了一半.
请教有没有好的办法?

解决方案 »

  1.   

    loop begin
    select nvl(mtrl_id,0) into v_mtrl_id 
    from table 
    where mtrl_id............; 
    exception when no_data_found then  --没有找到数据
      null;
    when too_many_rows then       --返回多于一条数据
      null;  --可改为自己的处理方式
    when others then       --其他错误
      null;  --可改为自己的处理方式
    end;end loop; 
      

  2.   

    不行啊,EXCEPTION不能在LOOP循环里用.
      

  3.   

    加入begin end 是可以用的。
      

  4.   

    Try the next pl/sql blockdeclare
      l_val number(1);
    begin
      for i in 1..10  loop     begin 
        
          select 1 
            into l_val 
            from dual
           where mod(i,2)=1;
        exception when no_data_found then
           dbms_output.put_line(i);
        end;   end loop; 
    end;
    /