Bulk Collect
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.
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:
= = = =
Loc = NEW YORK Loc = DALLAS SALES Loc = CHICAGO OPERATIONS Loc = BOSTON
ACCOUNTING RESEARCH
Ex:
DECLARE
= ' ||
END;
Output:
= = = =
Loc = NEW YORK Loc = DALLAS SALES Loc = CHICAGO OPERATIONS Loc = BOSTON
ACCOUNTING RESEARCH
You can use this to limit the number of rows to be fetched. Ex:
DECLARE
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
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:
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
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
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:
= = = =
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.