Skip to Main Content
  • Questions
  • when does collection.delete happens when bulk collecting

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Dinakar.

Asked: April 19, 2010 - 9:40 am UTC

Last updated: April 19, 2010 - 12:42 pm UTC

Version: 10.2.0

Viewed 1000+ times

You Asked

Hi Tom,
question regarding when does the collection element gets delete during fetch using bulk collect.

count in emp is 14. The eg code is as below:

set serveroutput on;
declare
type emp_type is table of emp%rowtype index by pls_integer;
lv_emp_type emp_type;
cursor c1 is select * from emp;
begin
open c1;
loop
fetch c1 bulk collect into lv_emp_type limit 1;
exit when lv_emp_type.count=0;
dbms_output.put_line('count in the collection::::'||lv_emp_type.count);
-----lv_emp_type.delete;
end loop;
end;

Note : lv_emp_type.delete is commented. The result of the above code is :

count in the collection::::1
count in the collection::::1
count in the collection::::1
count in the collection::::1
count in the collection::::1
count in the collection::::1
count in the collection::::1
count in the collection::::1
count in the collection::::1
count in the collection::::1
count in the collection::::1
count in the collection::::1
count in the collection::::1
count in the collection::::1

Since the line lv_emp_type.delete is commented, the count in the collection should have increased in every iteration as 1,2,3,4,5,6......14. But the collection element is getting deleted somehow? where does this delete happen?
I was always being told befeore the next loop delete the collection elements manually, but here its happening automatically. Please tell me more on this.

and Tom said...

everytime you bulk collect - the collection is reset.


I would code the above as:

loop
   fetch c1 bulk collect ...
   .... process any data found here ...
  
   EXIT WHEN C1%NOTFOUND;
end loop;


instead of the .count = 0 method.

Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Else PGA would burdened

V.Hariharaputhran, April 19, 2010 - 11:21 am UTC

It is exactly doing what it is suppose to do, if not just imagine the purpose of LIMIT would be meaning less.

Begin
............
cursor c1 is select * from emp;
begin
open c1;
loop
fetch c1 bulk collect into lv_emp_type limit 100;
exit when lv_emp_type.count=0;
end loop;
end;

If the bulk collect is going to pile data even by having limit as 100, it would over burden your PGA-memory and ultimately end with "out of memory".

Regards
V.Hari
Tom Kyte
April 19, 2010 - 11:28 am UTC

... if not just imagine the purpose
of LIMIT would be meaning less.
...


no, it wouldn't really, it would just mean that you SHOULD call "array.delete" first...




Bulk collection and Collection Delete

V.Hariharaputhran, April 19, 2010 - 12:16 pm UTC

Mr.Oracle
Preety fast reply :), Iam gifted.

You Said
.......you SHOULD call "array.delete" first

With respect to associative arrays,i really haven't done this array.delete step till date just before bulk fetch because i know the flow of the code.

Or Does it means that oracle internally does array.delete before performing bulk collect.

Or Do you mean freeing memory,making room every time before bulk fetch.

Iam getting confused with this statement indicating to perform array.delete before bulk fetch.

Regards
V.Hari
Tom Kyte
April 19, 2010 - 12:42 pm UTC

Or Does it means that oracle internally does array.delete before performing bulk collect.

Yes, a bulk collect empties the array AND THEN fills it again with 0 or more items.


It differs from a single row fetch which does not do that.


before you fetch bulk collect into l_array, you do not need to .delete it.



In the above, when I said should to the last poster, it was in response to their hypothetical situation, they said bulk collect would be useless if it did not work that way - I disagreed saying that IF it worked that when THEN you should call .delete


But it doesn't work in that hypothetical way, you need not call .delete to reset the array between bulk collects.

More to Explore

DBMS_OUTPUT

More on PL/SQL routine DBMS_OUTPUT here