遍历游标并将游标的输出存储在另一个 table

Iterate through cursor and storing the output of the cursor in another table

我正在尝试遍历存储 table 值的游标。我使用 FOR 循环进行迭代,如果满足其中一个条件,我将输出存储在另一个 table 中。我不确定我正在遵循的方法并且也收到错误(ORA-00933:SQL 命令未正确结束)。 Stats_Queries 是我的参考 table,我在其中迭代光标。 STATS_RESULT_CARD 是我的输出 table,我必须在其中存储结果。请帮忙

 DECLARE
     CURSOR c1 IS
       select Stats_Queries.OBJECTTYPE, Stats_Queries.CATEGORY, Stats_Queries.QUERY  
       from Stats_Queries;
    r1 c1%ROWTYPE;
    BEGIN
      FOR r1 IN c1 LOOP
        If (r1.OBJECTTYPE = 'CARD') THEN
        INSERT INTO STATS_RESULTS_CARD (NODETYPENAME, NODEDEFNAME , CARDTYPENAME, PROVISIONSTATUSNAME, STATDATE, CARDCOUNT)
        select nt.name, nd.name, ct.name, ps.name, sysdate, count(c.cardid)
    from cardtype ct, card c, node n, nodetype nt, nodedef nd, provisionstatus ps
    where ct.name in ('SRA AMP', 'XLA AMP', 'SAM', 'ESAM')
    and ct.cardtypeid = c.card2cardtype
    and c.card2node = n.nodeid
    and n.node2nodetype = nt.nodetypeid
    and n.node2nodedef = nd.nodedefid
    and c.card2provisionstatus = ps.provisionstatusid
    group by nt.name, nd.name, ct.name, ps.name
    END If;
    END LOOP;
    END;
ORA-00933: SQL command not ended properly

可能是因为您在

之后漏掉了一个分号
group by nt.name, nd.name, ct.name, ps.name

除了 Finbarr 提供的答案(完全正确;添加缺少的分号,您的程序应该可以工作)之外,您为什么需要遍历光标?这是做事的缓慢方式。

您可以只执行一个插入语句,例如:

  insert into stats_results_card (nodetypename,
                                  nodedefname,
                                  cardtypename,
                                  provisionstatusname,
                                  statdate,
                                  cardcount)
    select x.nt_name,
           x.nd_name,
           x.ct_name,
           x.ps_name,
           x.statdate,
           x.cnt_cardid                               
    from   (select   nt.name nt_name,
                     nd.name nd_name,
                     ct.name ct_name,
                     ps.name ps_name,
                     sysdate statdate,
                     count (c.cardid) cnt_cardid
            from     cardtype ct,
                     card c,
                     node n,
                     nodetype nt,
                     nodedef nd,
                     provisionstatus ps
            where        ct.name in ('SRA AMP',
                                     'XLA AMP',
                                     'SAM',
                                     'ESAM')
                     and ct.cardtypeid = c.card2cardtype
                     and c.card2node = n.nodeid
                     and n.node2nodetype = nt.nodetypeid
                     and n.node2nodedef = nd.nodedefid
                     and c.card2provisionstatus = ps.provisionstatusid
            group by nt.name,
                     nd.name,
                     ct.name,
                     ps.name) x
           cross join (select stats_queries.objecttype,
                              stats_queries.category,
                              stats_queries.query
                       from   stats_queries
                       where  objecttype = 'CARD');

N.B。这假设在原始游标和循环内的 select 语句之间确实没有任何 link;我们进行交叉连接以将行复制所需的次数。

如果两个查询之间存在实际连接,您可以用它代替交叉连接。