从光标到PL / SQL中的参数获取子查询值

时间:2018-04-17 03:03:09

标签: oracle plsql cursor subquery fetch

我有以下查询来选择子查询的计数:

SELECT COUNT(*) FROM (select crs_cust.CUSTOMER_ID, 
subset.NEW_REFERENCE_ID FROM CRS_CUSTOMERS crs_cust INNER JOIN DAY0_SUBSET subset ON crs_cust.CUSTOMER_ID=subset.CURRENT_CUSTOMER_ID);

以上查询是从PL / SQL中的游标执行的,如何从子查询(CUSTOMER_IDNEW_REFERENCE_ID)中将列提取到相应的参数中?

光标应该有多个记录。熟悉如下:

p_Count := SELECT COUNT(*) FROM DAY0_SUBSET;
OPEN c1; 
LOOP
FETCH c1 into p_Current_CustomerId,p_New_Cust_Ref_ID; -->query from cursor's subquery
EXIT WHEN c1%NOTFOUND;
EXIT WHEN (c1%ROWCOUNT <> p_Count);

FOR i IN c1 LOOP
<do manipulation of subquery values>
END LOOP;
END IF;
CLOSE c1;  

1 个答案:

答案 0 :(得分:0)

不会投影子查询的列,因此您无法引用它们。如果您希望程序中有CUSTOMER_IDNEW_REFERENCE_ID,则必须在顶级SELECT子句中选择它们。 解决方案最简单的答案就是打开光标 子查询本身,例如:

BEGIN
  FOR cur IN (SELECT crs_cust.CUSTOMER_ID, subset.NEW_REFERENCE_ID
               FROM CRS_CUSTOMERS crs_cust
                INNER JOIN DAY0_SUBSET subset ON 
                  crs_cust.CUSTOMER_ID=subset.CURRENT_CUSTOMER_ID)
  LOOP
     DBMS_OUTPUT.PUT_LINE(cur.customer_id || ', ' || cur.new_reference_id);
  END LOOP;
END;
/