我在同一个循环中有3个不同查询的3个sys_refcursor。 我想结合这3个游标的o / p并插入表中。 我正在使用下面的代码。" l_ssc"和" l_nwo"是记录。
SELECT shipper_short_code bulk collect
INTO l_ssc
FROM prepayment_ssc
ORDER BY shipper_short_code;
SELECT DISTINCT pmd_client_owner_nwo bulk collect
INTO l_nwo
FROM pp_meter_data ORDER pmd_client_owner_nwo;
FOR i IN 1.. l_ssc.count
LOOP
FOR j IN 1.. l_nwo.count
LOOP
l_query_a70 := 'select * from USER_PAYS_SI_A70 where shipper_short_code =''' || l_ssc(i).ssc ||''' and NWO_SHORT_CODE =''' ||l_nwo(j).nwo ||'''''';
l_query_O35 := 'select * from USER_PAYS_SI_O35 where NWO_SHORT_CODE=''' || l_nwo(j).nwo ||'';
l_query_R21 := 'select * from user_pays_si where SHIPPER_SHORT_CODE =''' || l_ssc(i).ssc ||''' and CLIENT_OWNER=''' || l_nwo(j).nwo ||'';
OPEN o_cursor1 FOR l_query_a70;
OPEN o_cursor2 FOR l_query_035;
OPEN o_cursor3 FOR l_query_r21;
END LOOP;
END LOOP;
/
答案 0 :(得分:2)
每次迭代循环运行3次查询,这将嵌套在两个循环中。因此,您将运行3 * l_ssc.count * l_nwo.count查询。
编写好的plsql的基本原则是让数据库为您执行处理。
找到另一种将查询编写为一个并让数据库完成循环工作的方法将创建一个更好的解决方案。
我没有你的桌子,但粗略的查询可能是:
select * from (
select upsa.*
from USER_PAYS_SI_A70 upsa
where upsa.shipper_short_code in (SELECT shipper_short_code FROM prepayment_ssc)
and upsa.NWO_SHORT_CODE in (SELECT DISTINCT pmd_client_owner_nwo FROM pp_meter_data)
union all
select upso.*
from USER_PAYS_SI_O35 upso
where upso.NWO_SHORT_CODE in (SELECT DISTINCT pmd_client_owner_nwo FROM pp_meter_data)
union all
select ups.*
from user_pays_si ups
where ups.SHIPPER_SHORT_CODE in (SELECT shipper_short_code FROM prepayment_ssc)
and ups.CLIENT_OWNER in (SELECT DISTINCT pmd_client_owner_nwo FROM pp_meter_data)
) so_far
order by so_far.SHIPPER_SHORT_CODE, so_far.NWO_SHORT_CODE
这不起作用,但通过一些实验,我确信您可以构建一个获得所需结果的查询。