运行游标循环时出现no_data_found错误

时间:2014-07-08 08:43:18

标签: database oracle stored-procedures cursors

每当我的游标循环中的任何select语句无法获取数据时,我都需要继续循环。我想处理相同的异常。此过程只要在连续性中找到数据就会插入数据,但只要游标选择的o.id不保存相关数据,它就会存在循环,并且只插入先前获取的记录的数据,并且不会继续循环。

CREATE OR REPLACE procedure order_violation1(
   p_type          number,
   p_code          varchar2,
   p_submit_from   date,
   p_submit_to     date,
   p_approved_from date,
   p_approved_to   date,
   p_flag          number,
   p_status        varchar2
) is 
  pp_type varchar2(100);
  pp_company varchar2(50);
  pp_code varchar2(20);
  pp_ord_num varchar2(50);
  pp_status varchar2(50);
  SUBMIT_DATE date;
  APPROVAL_DATE date;
  ORDERING_RATIO_FLAG number;
  pp_submit_date date;
  pp_app_date date;
  pp_package varchar2(3000);
  pp_flag NUMBER;

  cursor pp_id is
     select distinct o.id 
       from orders o,
            partnerprofile pp
      where type_id=p_type 
        and o.ordering_ratio_flag=p_flag 
        and pp.id=o.to_partner_id 
        and decode(P_CODE,null,'1',pp.code) = decode(P_CODE,null,'1',p_code)
        and decode(p_submit_from,null, to_date('01/01/01','dd/mm/yy'),
                   to_date(submit_date,'dd/mm/yy')) between 
            decode(p_submit_from ,null,
                   to_date('01/01/01','dd/mm/yy'),p_submit_from)  and 
            decode(p_submit_to,null,to_date('01/01/01','dd/mm/yy'),'05-JUL-14')
        and decode(p_approved_from,null,
                   to_date('01/01/01','dd/mm/yy'),
                   to_date(submit_date,'dd/mm/yy')) between
            decode(p_approved_from,null,
                   to_date('01/01/01','dd/mm/yy'),p_approved_from) and
            decode(p_approved_to,null,to_date('01/01/01','dd/mm/yy'),'05-JUL-14')
        and decode(p_status,null,'1',o.status) = decode(p_status,null,'1',p_status);
begin
  FOR r_partner IN pp_id
  loop
     select name 
       into pp_type 
       from  partnertype 
      where id=p_type;

     select code,
            company_name 
       into pp_code,
            pp_company 
       from partnerprofile pp,
            orders o 
      where o.id=r_partner.id 
        and pp.id=o.to_partner_id;

     select ORDER_NUMBER,
            STATUS,
            SUBMIT_DATE,
            APPROVAL_DATE,
            ORDERING_RATIO_FLAG 
       into pp_ord_num,
            pp_status,
            pp_submit_date,
            pp_app_date,
            pp_flag 
       from orders 
      where id=r_partner.id;

      select distinct
             rtrim (xmlagg (
                            xmlelement (e, pk.name||'='||
                                        nvl(oln.total_amount,0) || '||')
                            ).extract ('//text()'), ',') 
              into pp_package
        from  package pk,
              orderlineitem oln 
        where oln.package_id=pk.id 
          and oln.order_id=r_partner.id 
       GROUP  BY oln.order_id;

     insert into order_violation_tab1 
       values (pp_type, pp_code, pp_company, pp_ord_num,
               pp_status, pp_submit_date, pp_app_date,
               pp_flag, null, null);
     --pp_package);

  END;

1 个答案:

答案 0 :(得分:1)

正如尼古拉斯所指出的,以前的方法在这里不起作用。你必须在下面的循环中使用异常处理来处理这个问题。

 LOOP
     BEGIN
    -- select code
    exception
        when no_data_found then
          continue; 
     END;
     -- insert code
    END LOOP;

还继续;是11gr1及以上的功能,对于较旧的功能,你将不得不使用goto。

   LOOP
    BEGIN
    -- select code
    exception
        when no_data_found then
          goto label1;
     END;
    -- insert code 
    <<label1>>  
    null;
    END LOOP;
相关问题