Professional Documents
Culture Documents
Bulk Collect With PL
Bulk Collect With PL
Bulk Collect With PL
In PL/SQL, using bulk collect, it is possible to select the values from mutliple rows in one go.
Example #1
A table with two columns is created...
insert into bc values (11,'elf'); insert into bc values (12,'zwoelf'); insert into bc values (13,'dreizehn'); insert into bc values (14,'vierzehn');
commit;
An anonymous block is executed. Its declaration part defines two nested-table types and declares two variables, one of either type. In the body, the select statement used bulk collect to select all rows in the table in one go into the variables. Finally, a loop over the variables' elements prints the values.
l_bc_at_bc_a;
for i in l_bc_a.first .. l_bc_a.last loop dbms_output.put_line(l_bc_a(i) || ', ' || l_bc_b(i)); end loop;
Example #2
The 2nd example is similar to the first one. The main difference is that two columns are bulk collected into one (rather than two) variable. This variable must still be of a nested-table type. However, the type of the elements in the nested-table type must now be an object type with two member variables that correspond to the selected columns. Another difference is that the bulk collect will now be within a function that returns the values. First, the object type is created:
insert into vn values ('uno',1); insert into vn values ('due',2); insert into vn values ('tre',3);
commit;
The following function bulk collects the values of the table vnand returns it as a collection.
create or replace function fct_t return t_varchar_number as rett_varchar_number; begin selectvarchar_number(v,i) bulk collect into ret from vn;
for r in (select * from table(cast(x as t_varchar_number))) loop dbms_output.put_line(r.v || ', ' || r.i);
end loop;
drop type t_varchar_number; drop type varchar_number; drop function fct_t; drop table vn;
Example #3
This example demonstrates that bulk collects completly overrides (or deletes) the content of the variable selected into prior to the select.
create table bc_3 ( a number, b number ); insert into bc_3 values (1, 1); insert into bc_3 values (2, 1); insert into bc_3 values (3, 1);
insert into bc_3 values (4, 2); insert into bc_3 values (5, 2); insert into bc_3 values (6, 2); insert into bc_3 values (7, 2);
insert into bc_3 values (9, 3); declare type numbers is table of number;
numbers;
begin
select a bulk collect into n from bc_3 where b=1; select a bulk collect into n from bc_3 where b=3;
end; /
As the output shows, the result of the first bulk collect (where b=1) is completely overwritten by the second bulk collect (where b=3):
8 9