Batch Oracle binding forall bulk collect usage

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.