Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Bulk Fetch into a collection and use the collection inside a select statement

user6145802Jul 18 2021

Hi
Need a help with the below scenario only with PL/SQL.
I'm trying out the following:

  1. My table has 2M recs. Need to fetch 50K records in bulk into an object
  2. within the each iteration I want to refer the 50K records like a table inside a select statement and join with another table
    I don't want to loop through using forall / for loop

Eg. I've tried this:

Create type my_typ as object ( id1 Number(10), id2 Number(10) );
Create type my_typ_tbl as table of my_typ;
Declare
v_my_tbl my_typ_tbl;
cursor r is select rownum, object_id from all_objects;
begin
open r;
loop
fetch r
bulk collect
into v_my_tbl
limit 50000;
select count(*) into v_count from table(v_my_tbl); -- Just a sample the actual select combines other tables.
exit when r%notfound;
end loop;
end;
/

I seem to be combining 2 concepts of collections here and am running into errors.
How do I fetch the 50K records in bulk and then use them inside a select to fetch counts from another table.
Could someone guide me please.

Thanks in advance.
Sai

Comments
Post Details
Added on Jul 18 2021
4 comments
3,765 views