Using the RETURNING ...BULK COLLECT clause to SELECT directly into a PL/SQL array : bulk collect « PL SQL Statements « Oracle PL/SQL Tutorial






SQL>
SQL> create table employee(
  2           empl_no                integer         primary key
  3          ,lastname               varchar2(20)    not null
  4          ,firstname              varchar2(15)    not null
  5          ,midinit                varchar2(1)
  6          ,street                 varchar2(30)
  7          ,city                   varchar2(20)
  8          ,state                  varchar2(2)
  9          ,zip                    varchar2(5)
 10          ,zip_4                  varchar2(4)
 11          ,area_code              varchar2(3)
 12          ,phone                  varchar2(8)
 13          ,company_name           varchar2(50));

Table created.

SQL>
SQL>
SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4,area_code,phone,company_name)
  2  values(1,'Jones','Joe','J','10 Ave','New York','NY','11111','1111','111', '111-1111','A Company');

1 row created.

SQL>
SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4,area_code,phone,company_name)
  2  values(2,'Smith','Sue','J','20 Ave','New York','NY','22222','2222','222', '222-111','B Company');

1 row created.

SQL>
SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4,area_code,phone,company_name)
  2  values(3,'Anderson','Peggy','J','500 St','New York','NY','33333','3333','333', '333-3333','C Company');

1 row created.

SQL>
SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4,area_code,phone,company_name)
  2  values(4,'Andy','Jill', null,'930 St','New York','NY','44444','4444','212', '634-7733','D Company');

1 row created.

SQL>
SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4,area_code,phone,company_name)
  2  values(5,'OK','Carl','L','19 Drive','New York','NY','55555','3234','212', '243-4243','E Company');

1 row created.

SQL>
SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4,area_code,phone,company_name)
  2  values(6,'Peter','Jee','Q','38 Ave','New York','NY','66666','4598','212', '454-5443','F Inc');

1 row created.

SQL>
SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4,area_code,phone,company_name)
  2  values(7,'Baker','Paul','V','738 St.','Queens','NY','77777','3842','718', '664-4333','G Inc');

1 row created.

SQL>
SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4,area_code,phone,company_name)
  2  values(8,'Young','Steve','J','388 Ave','New York','NY','88888','3468','212', '456-4566','H Associates Inc');

1 row created.

SQL>
SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4,area_code,phone,company_name)
  2  values(9,'Mona','Joe','T','9300 Ave','Kansas City','MO','99999','3658','415', '456-4563','J Inc');

1 row created.

SQL>
SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4,area_code,phone,company_name)
  2  values(10,'Hackett','Karen','S','Kings Rd. Apt 833','Bellmore','NY','61202','3898','516', '767-5677','AA Inc');

1 row created.

SQL>
SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4,area_code,phone,company_name)
  2  values(11,'Bob','Jack','S','12 Giant Rd.','Newark','NJ','27377','3298','908', '123-7367','Z Associates');

1 row created.

SQL>
SQL> DECLARE
  2      Type cust_rec_t is RECORD
  3      (empl_no  number, lastname varchar2(25),
  4                firstname varchar2(25) );
  5      Type cust_tab_t is table of cust_rec_t
  6                   index by binary_integer;
  7      cust_tab  cust_tab_t;
  8  BEGIN
  9      UPDATE employee SET area_code = '917'
 10      WHERE area_code = '212'
 11      RETURNING empl_no, lastname, firstname
 12      BULK COLLECT INTO cust_tab;
 13      dbms_output.put_line(cust_tab(1).lastname );
 14      dbms_output.put_line(cust_tab.COUNT );
 15  END;
 16  /
Andy
4

PL/SQL procedure successfully completed.

SQL>
SQL>
SQL>
SQL>
SQL> drop table employee;

Table dropped.

SQL>
SQL>
SQL>
SQL>








22.17.bulk collect
22.17.1.select BULK COLLECT
22.17.2.Adding a limit to BULK COLLECT
22.17.3.To get the same result by using explicit cursors and bulk collect
22.17.4.Speeding Up Data Collection with Bulk Operations
22.17.5.Using the RETURNING ...BULK COLLECT clause to SELECT directly into a PL/SQL array
22.17.6.Demonstrate how to use DELETE with bulk processing.
22.17.7.Bulk processing with the FORALL statement
22.17.8.Bulk collect with non-cursor SELECT into multiple collections
22.17.9.Bulk collect from cursor with LIMIT option