关于数据库:运行游标循环时出现 no_data_found 错误

no_data_found error when running a cursor loop

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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;


正如 Nicholas 指出的那样,以前的方法在这里不起作用。您将不得不在如下所示的循环内使用异常处理来处理此问题。

1
2
3
4
5
6
7
8
9
 LOOP
     BEGIN
    -- select code
    exception
        when no_data_found then
          continue;
     END;
     -- insert code
    END LOOP;

也继续;是 11gr1 及更高版本的功能,对于较旧的,您将不得不使用 goto。

1
2
3
4
5
6
7
8
9
10
11
   LOOP
    BEGIN
    -- select code
    exception
        when no_data_found then
          goto label1;
     END;
    -- insert code
    <<label1>>  
    null;
    END LOOP;