I have kind of a tricky Oracle problem. I am trying to select one set of data, we'll call items. For each item I want to call another procedure and return an Inventory Item. I have two operations I am not sure on how to perform.
How do I retrieve a value from the nested procedure?
How do I return those retrieved values in the form of SYS_REFCURSOR?
My attempt here was to put the results from spSelect_Inv_Search into a nested table called ITEMS_TABLE. This is not working.
Code below
PROCEDURE SPSELECT_ITEM (IO_CURSOR OUT SYS_REFCURSOR)
AS
MY_CURSOR SYS_REFCURSOR;
TYPE ITEM_TYPE IS TABLE OF ITEMS.ITEM_NO%TYPE;
ITEM_TABLE ITEM_TYPE := ITEM_TYPE();
CURSOR ITEMS_CURSOR IS
SELECT ITEM_NO
FROM ITEMS;
V_COUNTER INTEGER := 0;
BEGIN
FOR ITEM_REC IN ITEM_CURSOR LOOP
V_COUNTER := V_COUNTER + 1;
ITEM_TABLE.EXTEND;
ITEM_TABLE(V_COUNTER) := spSelect_Inv_Search(ITEM_REC.ITEM_NO, MY_CURSOR);
END LOOP;
END SPSELECT_ITEMS;
Any help is appreciated, thanks.
You seem to be wanting to merge an unknown number of
SYS_REFCURSOR
result sets into one big one. If you know the structure of the cursor returned fromspSelect_Inv_Search
you can do this with an intermediate pipelined function.The types can be defined here, they don't have to be at SQL level as you won't ever need to reference them outside the package.
Then to execute, which you can do outside the package despite the types used for the intermediate stage, you can do this to test in SQL*Plus or SQL Developer:
For my database this gives:
This is obviously very contrived as you'd do this as a single query, but I'm assuming your inner procedure needs to do something more complicated.
In to answer your question about how to call
spSelect_Inv_Search
, I'd need to know the signature of that subprogram. You've described it as a procedure but you're trying to call it as a function. Which is it? What return value and/or OUT-mode parameters does it have?To return an open REF CURSOR from the above procedure, first the nested table type needs to be declared at the schema level (using a
CREATE TYPE
statement) instead of in the PL/SQL code. Then you can open the cursor like so, after populating the nested table.(And by the way, I would change the name of the type from
ITEM_TYPE
toITEM_TABLE_TYPE
, myself.)