0% found this document useful (0 votes)
73 views

Bulk Collect

Bulk collect allows retrieving multiple rows from a database table into PL/SQL collections or records with a single roundtrip, improving performance. It can be used with SELECT, FETCH, and RETURNING INTO clauses. Collections referenced in the bulk collect clause are automatically initialized and extended. The LIMIT clause restricts the number of rows returned. Multiple collections can be populated with one column each. Data can be returned to collections using the RETURNING clause.

Uploaded by

adinesh262
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

Bulk Collect

Bulk collect allows retrieving multiple rows from a database table into PL/SQL collections or records with a single roundtrip, improving performance. It can be used with SELECT, FETCH, and RETURNING INTO clauses. Collections referenced in the bulk collect clause are automatically initialized and extended. The LIMIT clause restricts the number of rows returned. Multiple collections can be populated with one column each. Data can be returned to collections using the RETURNING clause.

Uploaded by

adinesh262
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

BULK COLLECT

This is used for array fetches With this you can retrieve multiple rows of data with a single roundtrip. This reduces the number of context switches between the pl/sql and sql engines. Reduces the overhead of retrieving data. You can use bulk collect in both dynamic and static sql. You can use bulk collect in select, fetch into and returning into clauses. SQL engine automatically initializes and extends the collections you reference in the bulk collect clause. Bulk collect operation empties the collection referenced in the into clause before executing the query. You can use the limit clause of bulk collect to restrict the no of rows retrieved. You can fetch into multiple collections with one column each. Using the returning clause we can return data to another collection.

BULK COLLECT IN FETCH

Ex:

DECLARE

BEGIN

Type t is table of dept%rowtype; nt t; Cursor c is select *from dept; Open c; Fetch c bulk collect into nt; Close c; For i in nt.first..nt.last loop dbms_output.put_line('Dname = ' || nt(i).dname || ' Loc end loop; nt(i).loc);

= ' ||

END;

Output:

Dname Dname Dname Dname

= = = =

Loc = NEW YORK Loc = DALLAS SALES Loc = CHICAGO OPERATIONS Loc = BOSTON
ACCOUNTING RESEARCH

BULK COLLECT IN SELECT

Ex:

DECLARE

Type t is table of dept%rowtype; Nt t;


BEGIN

Select * bulk collect into nt from dept; for i in nt.first..nt.last loop

= ' ||
END;

dbms_output.put_line('Dname = ' || nt(i).dname || ' Loc end loop; nt(i).loc);

Output:

Dname Dname Dname Dname

= = = =

Loc = NEW YORK Loc = DALLAS SALES Loc = CHICAGO OPERATIONS Loc = BOSTON
ACCOUNTING RESEARCH

LIMIT IN BULK COLLECT

You can use this to limit the number of rows to be fetched. Ex:
DECLARE

Type t is table of dept%rowtype; nt t; Cursor c is select *from dept;

Open c; Fetch c bulk collect into nt limit 2; Close c; For i in nt.first..nt.last loop dbms_output.put_line('Dname = ' || nt(i).dname || ' Loc = ' || nt(i).loc); end loop;
END;

BEGIN

Output:

Dname = Dname =

ACCOUNTING RESEARCH

Loc = NEW YORK Loc = DALLAS

MULTIPLE FETCHES IN INTO CLAUSE

Ex1:

DECLARE

BEGIN

Type t is table of dept.dname%type; nt t; Type t1 is table of dept.loc%type; nt1 t; Cursor c is select dname,loc from dept; Open c; Fetch c bulk collect into nt,nt1; Close c; For i in nt.first..nt.last loop dbms_output.put_line('Dname = ' || nt(i)); end loop; For i in nt1.first..nt1.last loop dbms_output.put_line('Loc = ' || nt1(i)); end loop;

END;

Output:

Dname = ACCOUNTING Dname = RESEARCH Dname = SALES Dname = OPERATIONS Loc = NEW YORK Loc = DALLAS Loc = CHICAGO Loc = BOSTON
DECLARE

Ex2:

type t is table of dept.dname%type; type t1 is table of dept.loc%type; nt t; nt1 t1;


BEGIN

Select dname,loc bulk collect into nt,nt1 from dept; for i in nt.first..nt.last loop dbms_output.put_line('Dname = ' || nt(i)); end loop; for i in nt1.first..nt1.last loop dbms_output.put_line('Loc = ' || nt1(i)); end loop;

END;

Output:

Dname = ACCOUNTING Dname = RESEARCH Dname = SALES Dname = OPERATIONS Loc = NEW YORK Loc = DALLAS Loc = CHICAGO Loc = BOSTON

RETURNING CLAUSE IN BULK COLLECT

You can use this to return the processed data to the output variables or typed variables. Ex:
DECLARE

BEGIN

type t is table of number(2); nt t := t(1,2,3,4); type t1 is table of varchar(2); nt1 t1; type t2 is table of student%rowtype; nt2 t2; select name bulk collect into nt1 from student; forall v in nt1.first..nt1.last

returning

update student set no = nt(v) where name = nt1(v)

no, name, marks bulk collect into nt2; for v in nt2.first..nt2.last loop dbms_output.put_line('Marks = ' || nt2(v)); end loop;
END;

Output:

Marks Marks Marks Marks

= = = =

100 200 300 400

POINTS TO REMEMBER Cursor name can be up to 30 characters in length. Cursors declared in anonymous blocks or subprograms closes automatically when that block terminates execution. %bulk_rowcount and %bulk_exceptions can be used only with forall construct. Cursor declarations may have expressions with column aliases. These expressions are called virtual columns or calculated columns.

You might also like