Bulk collect can be used to load the query results to collections at a time, instead of processing them one by one Using CURSOR. You can use bulk collect in select into, fetchinto, and returning into statements.
Note that when bulk collect is used, all into variables must be collections.
---- Use bulk collect in select into statement
DECLARE
TYPE SalList is table of emp. sal % TYPE;
Sals SalList;
BEGIn
SELECT sal bulk collect into sals FROM emp where rownum <= 100;
SELECT sal bulk collect into sals FROM emp SAMPLE 10;
---- Use bulk collect in fetch
DECLARE
TYPE deptredtd is table of dept % ROWTYPE;
Dept_recs deptrectl;
CURSOR c1 IS
SELECT deptno, dname, loc FROM dept WHERE deptno> 10;
BEGIN
OPEN c1;
FETCH c1 bulk collect into dept_recs;
END;
/
---- Use bulk collect in returning
Create table emp2 as select * FROM employees;
DECLARE
TYPE NumList is table of employees. employee_id % TYPE;
Enums NumList;
TYPE NameList is table of employees. last_name % TYPE;
Names NameList;
BEGIN
Delete from emp2 WHERE department_id = 30;
RETURNING employee_id, last_name bulk collect into enums, names;
DBMS_OUTPUT.PUT_LINE ('deleted' | SQL % ROWCOUTN | 'rows ;');
FOR I IN enms. FIRST... enums. LAST
LOOP
DBMS_OUTPUT.PUT_LINE ('employee # '| enums (I) |'; '| names (I ));
End loop;
END;
/
Oracle batch binding forall bulk collect
Bulk binds improves performance by reducing context switches between pl/SQL and SQL engines.
Bulk binds includes:
(I) Input collections, use the FORALL statement to Improve the Performance of DML (INSERT, UPDATE, and DELETE) operations.
(Ii) Output collections, use the bulk collect clause, which is generally used to improve the query (SELECT) Performance