You are on page 1of 2

pl/sql table type variable and how to use bulk collect and select to that variable

BULK COLLECT: to do the bulk DML operations


to overcome the context swithcing execution method , oracle has introduced bulk
bind through & uses bulk collect by using collections
we can use bulk collect keyword in 2 ways
select into clause
when opening cursor with FORALL
How is performance improved with Bulk Collect?
* Bulk Collect fetches large number of rows in to a collection (rather than one row
at a time) and process it.
This improves the performance and reduces the time when compared to processing
each and every row.
Especially when a program does a DML, use of Bulk collect and FORALL will improve
the performance.
How to determine the limit value of Bulk Collect?
* The limit is clause is optional and is placed in the FETCH statement. This
determines the number of rows to be collected.
It is determined based on the size of the result fetched and the process memory
available. If result set has 1000 rows.
You can make the limit to 100, it runs 10 times and process all the 1000 records.
When you set the limit higher than the memory available, out of process memory
exception will be thrown.
Say 100 rows are inserted in to the table using bulk collect. How to continue
inserting when there are exception in 50th record?
* by using SAVE EXCEPTIONS it there is any error, it saves the exception details,
skips the record and continue with the update/ insert.
errors encountered while using BULK COLECT
1. Out of Process memory error is thrown out when there is no enough space to hold
the collection.
2. If a collection holds 100 items, and the 50th item is deleted. Then there is a
nothing in the place of 50th item.
It fails on seeing a null value in the collection.
attributes
1) %BULK_ROWCOUNT:-
%BULK_ROWCOUNT(i) stores the number of rows processed by the ith execution of an
DML statement.
If the ith execution affects no rows, %BULK_ROWCOUNT(i) returns zero.
2) %BULK_EXCEPTIONS:-
It is an associative array that stores information about any exceptions encountered
when using the SAVE EXCEPTIONS clause.
SQL%BULK_EXCEPTIONS.COUNT: Represents the number of exceptions.
SQL%BULK_EXCEPTIONS(i).ERROR_INDEX Specifies which item in the collection caused
the exception
SQL%BULK_EXCEPTIONS(i).ERROR_CODE Specifies the Oracle error code that corresponds
to the exception.

DECLARE
CURSOR c_sample IS SELECT ename FROM emp;
TYPE lv_ename_tbl IS TABLE OF VARCHAR2(50);
lv_ename lv_ename_tbl;
BEGIN
OPEN c_sample;
FETCH c_sample BULK COLLECT INTO lv_ename LIMIT 5000;
FOR c_ename IN lv_ename.FIRST .. lv_ename.LAST
LOOP
Dbms_output.put_line(‘Employee Fetched:‘||c_ename);
END LOOP:
FORALL i IN lv_ename.FIRST .. lv ename.LAST
UPDATE emp SET salaiy=salary+5000 WHERE ename=lv_ename(i);
COMMIT;
Dbms_output.put_line(‘Salary Updated‘);
CLOSE c_sample;
END;

collections
nested table
varray
index by table

You might also like