Here is the Procedure:
- Opening a cursor and then fetching the output of select query through bulk collect.
- Issue is all the ID's are getting stored in bulk collect but I am unable to loop through the second select query by using the bulk collect variable as input, It only takes first ID into consideration instead of all.
- OUTPUT should be a SYS_REFCURSOR, please shed light on what am I missing here
test data for table1:
ID | CURRENCY | T_ID |
---|---|---|
10 | GBP | PB1 |
15 | GBP | RB |
20 | GBP | CC |
25 | AUD | DC |
Based on the t_id I am fetching the corresponding ID's and then using those ID's in further select for loop statements.
CURRENT OUPUT OF THE PROC THROUGH SYS_REFCURSOR:
ID | COUNTRY | ACCOUNT |
---|---|---|
10 | UK | PB1 |
EXPECTED OUTPUT:
ID | COUNTRY | ACCOUNT |
---|---|---|
10 | UK | PB1 |
15 | Wales | RB |
20 | SH | CC |
create or replace procedure myproc (i_id in varchar2, rc out sys_refcursor)
as
cursor names_cur is
select id from table1 where currency='GBP' and t_id=i_id;
names_t names_cur%ROWTYPE;
type names_ntt is table of names_t%TYPE;
l_names names_ntt;
begin
open names_cur;
fetch names_cur bulk collect into l_names ; --Inside l_names (10,15 & 20) would be stored
close names_cur;
--iSSUE IS WITH BELOW FOR LOOP
for cur in l_names.first..l_names.last loop
open rc for --For the below select I want to iterate l_names so for the above scenario it should iterate thrice
select s.id,s.country,s.account from table2 s where s.id=l_names(cur).id;
end loop;
end myproc;