Fetch subquery value from cursor to parameter in PL/SQL

末鹿安然 提交于 2019-12-11 14:22:18

问题


I have below query to select the count of subquery:

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);

Above query is executed from a cursor in PL/SQL, how can I fetch columns from subquery (CUSTOMER_ID and NEW_REFERENCE_ID) into respective parameter?

The cursor is expected to have multiple records.Similiar like the following:

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:


The columns of sub queries are not projected so you can't reference them. If you want the CUSTOMER_ID and NEW_REFERENCE_ID in your program you will have to select them in the top level SELECT clause. The easiest answer to your solution is to just open a cursor for your subquery itself, for eample:

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;
/


来源:https://stackoverflow.com/questions/49869208/fetch-subquery-value-from-cursor-to-parameter-in-pl-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!