我正在尝试遍历存储表值的游标。我使用FOR循环进行迭代,如果满足其中一个条件,我将输出存储在另一个表中。我不确定我正在遵循的方法并且还得到错误(ORA-00933:SQL命令未正确结束)。 Stats_Queries是我的参考表,我在其中迭代我的光标。 STATS_RESULT_CARD是我的输出表,我必须存储结果。请帮忙。
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;
答案 0 :(得分:1)
ORA-00933: SQL command not ended properly
可能是因为你错过了分号后的
group by nt.name, nd.name, ct.name, ps.name
答案 1 :(得分:1)
除了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语句之间确实没有任何链接;我们进行交叉连接以复制行所需的次数。
如果两个查询之间存在实际连接,则应将其替换为交叉连接。