Reputation: 1525
I'm trying to return two ref cursors from a procedure and having a bit of trouble. What I'm trying to do is grab the info from the first cursor, select a few fields out of it and join to some other info and stick the result into a table variable... then select distinct items from that table into the second cursor. But I can't get this to compile. Can someone tell me what I'm doing wrong please?
type T_CURSOR is REF CURSOR
procedure FetchSL3Details_PRC
(
c_items out T_CURSOR,
c_identifiers out T_CURSOR,
p_niin in char
) as
v_idents IDENTIFIER_TABLE_TYPE:= IDENTIFIER_TABLE_TYPE();
BEGIN
open c_items for
select
its.item_set_id,
its.niin,
its.parent_niin,
its.commodity_id,
its.service_type,
its.sl3_type,
its.qty,
its.created_id,
its.created_dt,
its.modified_id,
its.modified_dt
from
item_set its
start with its.niin = p_niin
connect by prior its.niin = its.parent_niin;
for item in c_items loop
v_idents.extend;
v_idents(v_idents.LAST) := identifier_row_type(item.commodity_id,
get_group_name_fun(item.commodity_id),
0);
v_idents.extend;
v_idents(v_idents.LAST) := identifier_row_type(item.created_id,
get_formatted_name_fun(item.created_id),
0);
v_idents.extend;
v_idents(v_idents.LAST) := identifier_row_type(item.modified_id,
get_formatted_name_fun(item.modified_id),
0);
end loop;
open c_identifiers for
select
distinct(v.id),
v.name,
v.type
from
v_idents v;
END FetchSL3Details_PRC;
Upvotes: 2
Views: 2068
Reputation: 48111
You can't use this construct:
for item in c_items loop
with a REF CURSOR. It expects c_items
to be a standard PL/SQL CURSOR
. That's the immediate cause of the error you're getting. If you want to loop over a REF CURSOR, as far as I know, you need to use explicit FETCH statements and handle the loop condition yourself.
Furthermore, what you say you are trying to do doesn't quite make sense. If you fetch from the c_items
cursor within the body of the procedure, returning it to the caller as well is confusing. In your comment, you use the phrase "select into the cursor", which implies that maybe you think of the cursor as a static collection that you can iterate over repeatedly. This is not the case -- a cursor represents an active query in memory. Once a row is fetched from the cursor, it can't be fetched again.
I'm not sure what to suggest exactly since I don't understand the end goal of the code. If you really need to both process the rows from c_items
and return it as a usable REF CURSOR, then the only option may be to close and re-open it.
Upvotes: 4
Reputation: 132570
Change this:
open c_identifiers for
select
distinct(v.id),
v.name,
v.type
from
v_idents v;
to:
open c_identifiers for
select
distinct(v.id),
v.name,
v.type
from
TABLE(v_idents) v; -- Use TABLE
Upvotes: 1