Sie sind auf Seite 1von 25

22. 17. 1.

select BULK COLLECT


SQL> create table cities 2 as 3 select username city 4 from all_users 5 where rownum<=37; Table created. SQL> SQL> alter table cities 2 add constraint 3 cities_pk primary key(city); Table altered. SQL> SQL> create table with_ri 2 ( x char(80), 3 city references cities 4 ); Table created. SQL> SQL> create table without_ri 2 ( x char(80), 3 city varchar2(30) 4 ); Table created. SQL> alter session set sql_trace=true; Session altered. SQL> SQL> declare 2 type array is table of varchar2(30) index by binary_integer; 3 l_data array; 4 begin 5 select * BULK COLLECT into l_data from cities; 6 for i in 1 .. 1000 7 loop 8 for j in 1 .. l_data.count 9 loop 10 insert into with_ri 11 values ('x', l_data(j) ); 12 insert into without_ri 13 values ('x', l_data(j) ); 14 end loop; 15 end loop; 16 end;

17

PL/SQL procedure successfully completed. SQL> SQL> SQL> drop table cities cascade constraint; Table dropped. SQL> drop table with_ri; Table dropped. SQL> drop table without_ri; Table dropped.

22. 17. 2. Adding a limit to BULK COLLECT


SQL> SQL> SQL> SQL> 2 3 4 5 6 7 8 9 10 11

-- create demo table create table Employee( ID VARCHAR2(4 BYTE) First_Name VARCHAR2(10 BYTE), Last_Name VARCHAR2(10 BYTE), Start_Date DATE, End_Date DATE, Salary Number(8,2), City VARCHAR2(10 BYTE), Description VARCHAR2(15 BYTE) ) /

NOT NULL,

Table created.

SQL> SQL> -- prepare data SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_ 2 values ('01','Jason', 'Martin', to_date('19960725','YYYYMMDD'), to_d 3 / 1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_ 2 values('02','Alison', 'Mathews', to_date('19760321','YYYYMMDD'), to_d 3 / 1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_ 2 values('03','James', 'Smith', to_date('19781212','YYYYMMDD'), to_d 3 /

1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_ 2 values('04','Celia', 'Rice', to_date('19821024','YYYYMMDD'), to_d 3 / 1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_ 2 values('05','Robert', 'Black', to_date('19840115','YYYYMMDD'), to_d 3 / 1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_ 2 values('06','Linda', 'Green', to_date('19870730','YYYYMMDD'), to_d 3 / 1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_ 2 values('07','David', 'Larry', to_date('19901231','YYYYMMDD'), to_d 3 / 1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_ 2 values('08','James', 'Cat', to_date('19960917','YYYYMMDD'), to_d 3 / 1 row created. SQL> SQL> SQL> SQL> -- display data in the table SQL> select * from Employee 2 / ID ---01 02 03 04 05 06 07 08 FIRST_NAME -------------------Jason Alison James Celia Robert Linda David James LAST_NAME -------------------Martin Mathews Smith Rice Black Green Larry Cat START_DAT --------25-JUL-96 21-MAR-76 12-DEC-78 24-OCT-82 15-JAN-84 30-JUL-87 31-DEC-90 17-SEP-96 END_DATE SALARY CITY --------- ---------- ---------25-JUL-06 1234.56 Toronto 21-FEB-86 6661.78 Vancouver 15-MAR-90 6544.78 Vancouver 21-APR-99 2344.78 Vancouver 08-AUG-98 2334.78 Vancouver 04-JAN-96 4322.78 New York 12-FEB-98 7897.78 New York 15-APR-02 1232.78 Vancouver

D P T T M T T M T

8 rows selected. SQL> SQL> SQL>

SQL> declare 2 type text_nt is table of VARCHAR2(256); 3 v_ename_nt text_nt; 4 cursor c_emp is select first_name from employee where id='02'; 5 procedure p_print_row is 6 begin 7 if v_eName_nt.count=2 then 8 DBMS_OUTPUT.put_line(v_eName_nt(1)||' '||v_eName_nt(2)); 9 elsif v_eName_nt.count=1 then 10 DBMS_OUTPUT.put_line(v_eName_nt(1)); 11 end if; 12 end; 13 begin 14 open c_emp; 15 loop 16 fetch c_emp bulk collect into v_eName_nt limit 2; 17 p_print_row; 18 exit when c_emp%NOTFOUND; 19 end loop; 20 close c_emp; 21 end; 22 / Alison PL/SQL procedure successfully completed. SQL> SQL> SQL> SQL> SQL> -- clean the table SQL> drop table Employee 2 / Table dropped.

22. 17. 3. To get the same result by using explicit cursors and bulk collect
SQL> SQL> SQL> 2 3 4 5 6 7 8 9 10 11 -- create demo table create table Employee( ID VARCHAR2(4 BYTE) First_Name VARCHAR2(10 BYTE), Last_Name VARCHAR2(10 BYTE), Start_Date DATE, End_Date DATE, Salary Number(8,2), City VARCHAR2(10 BYTE), Description VARCHAR2(15 BYTE) ) /

NOT NULL,

Table created.

SQL> SQL> -- prepare data SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_ 2 values ('01','Jason', 'Martin', to_date('19960725','YYYYMMDD'), to_d 3 / 1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_ 2 values('02','Alison', 'Mathews', to_date('19760321','YYYYMMDD'), to_d 3 / 1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_ 2 values('03','James', 'Smith', to_date('19781212','YYYYMMDD'), to_d 3 / 1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_ 2 values('04','Celia', 'Rice', to_date('19821024','YYYYMMDD'), to_d 3 / 1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_ 2 values('05','Robert', 'Black', to_date('19840115','YYYYMMDD'), to_d 3 / 1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_ 2 values('06','Linda', 'Green', to_date('19870730','YYYYMMDD'), to_d 3 / 1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_ 2 values('07','David', 'Larry', to_date('19901231','YYYYMMDD'), to_d 3 / 1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_ 2 values('08','James', 'Cat', to_date('19960917','YYYYMMDD'), to_d 3 / 1 row created. SQL> SQL> SQL> SQL> -- display data in the table SQL> select * from Employee 2 /

ID ---01 02 03 04 05 06 07 08

FIRST_NAME -------------------Jason Alison James Celia Robert Linda David James

LAST_NAME -------------------Martin Mathews Smith Rice Black Green Larry Cat

START_DAT --------25-JUL-96 21-MAR-76 12-DEC-78 24-OCT-82 15-JAN-84 30-JUL-87 31-DEC-90 17-SEP-96

END_DATE SALARY CITY --------- ---------- ---------25-JUL-06 1234.56 Toronto 21-FEB-86 6661.78 Vancouver 15-MAR-90 6544.78 Vancouver 21-APR-99 2344.78 Vancouver 08-AUG-98 2334.78 Vancouver 04-JAN-96 4322.78 New York 12-FEB-98 7897.78 New York 15-APR-02 1232.78 Vancouver

D P T T M T T M T

8 rows selected. SQL> SQL> SQL> SQL> create or replace type emp2_oty is object (empNo NUMBER,eName VARCHAR2(10)) 2 / Type created. SQL> SQL> create or replace type emp2_nt is table of emp2_oty 2 / Type created. SQL> SQL> SQL> 2 3 4 5 6 7 8 9 10 11 12

create or replace function f_getEmpDept_nt (i_deptNo varchar) return emp2_nt is v_emp2_nt emp2_nt:=emp2_nt(); cursor c_emp is select emp2_oty(id, first_Name) from employee where id=i_deptNo; begin open c_emp; fetch c_emp bulk collect into v_emp2_nt; close c_emp; return v_emp2_nt; end; /

Function created. SQL> SQL> drop type emp2_nt; Type dropped. SQL> SQL> drop type emp2_oty; Type dropped. SQL>

SQL> SQL> SQL> SQL> -- clean the table SQL> drop table Employee 2 / Table dropped.

22. 17. 4. Speeding Up Data Collection with Bulk Operations


SQL> SQL> SQL> 2 3 4 5 6 7 8 9 10 11 -- create demo table create table Employee( ID VARCHAR2(4 BYTE) First_Name VARCHAR2(10 BYTE), Last_Name VARCHAR2(10 BYTE), Start_Date DATE, End_Date DATE, Salary Number(8,2), City VARCHAR2(10 BYTE), Description VARCHAR2(15 BYTE) ) /

NOT NULL,

Table created.

SQL> SQL> -- prepare data SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End 2 values ('01','Jason', 'Martin', to_date('19960725','YYYYMMDD'), to_ 3 / 1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End 2 values('02','Alison', 'Mathews', to_date('19760321','YYYYMMDD'), to_ 3 / 1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End 2 values('03','James', 'Smith', to_date('19781212','YYYYMMDD'), to_ 3 / 1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End 2 values('04','Celia', 'Rice', to_date('19821024','YYYYMMDD'), to_ 3 /

1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End 2 values('05','Robert', 'Black', to_date('19840115','YYYYMMDD'), to_ 3 / 1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End 2 values('06','Linda', 'Green', to_date('19870730','YYYYMMDD'), to_ 3 / 1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End 2 values('07','David', 'Larry', to_date('19901231','YYYYMMDD'), to_ 3 / 1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End 2 values('08','James', 'Cat', to_date('19960917','YYYYMMDD'), to_ 3 / 1 row created. SQL> SQL> SQL> SQL> -- display data in the table SQL> select * from Employee 2 / ID ---01 02 03 04 05 06 07 08 FIRST_NAME -------------------Jason Alison James Celia Robert Linda David James LAST_NAME -------------------Martin Mathews Smith Rice Black Green Larry Cat START_DAT --------25-JUL-96 21-MAR-76 12-DEC-78 24-OCT-82 15-JAN-84 30-JUL-87 31-DEC-90 17-SEP-96

END_DATE SALARY CITY --------- ---------- ---------25-JUL-06 1234.56 Toronto 21-FEB-86 6661.78 Vancouver 15-MAR-90 6544.78 Vancouver 21-APR-99 2344.78 Vancouver 08-AUG-98 2334.78 Vancouver 04-JAN-96 4322.78 New York 12-FEB-98 7897.78 New York 15-APR-02 1232.78 Vancouver

8 rows selected. SQL> SQL> SQL> SQL> create or replace type emp2_oty is object (empNo NUMBER,eName VARCHAR2(10)) 2 / Type created. SQL>

SQL> create or replace type emp2_nt is table of emp2_oty 2 / Type created. SQL> SQL> SQL> 2 3 4 5 6 7 8

create or replace function f_getEmpDept_nt (i_deptNo VARCHAR) return emp2_nt is v_emp2_nt emp2_nt:=emp2_nt(); begin select emp2_oty(id, first_name) bulk collect into v_emp2_nt from employee where return v_emp2_nt; end; /

Function created. SQL> SQL> drop type emp2_nt; Type dropped. SQL> SQL> drop type emp2_oty; Type dropped. SQL> SQL> SQL> SQL> SQL> SQL> -- clean the table SQL> drop table Employee 2 / Table dropped.

22. 17. 5. Using the RETURNING ...BULK COLLECT clause to SELECT array
SQL> SQL> create table employee( 2 empl_no 3 ,lastname 4 ,firstname 5 ,midinit 6 ,street 7 ,city 8 ,state 9 ,zip

integer varchar2(20) varchar2(15) varchar2(1) varchar2(30) varchar2(20) varchar2(2) varchar2(5)

primary key not null not null

10 11 12 13

,zip_4 ,area_code ,phone ,company_name

varchar2(4) varchar2(3) varchar2(8) varchar2(50));

Table created.

SQL> SQL> SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4, ) 2 values(1,'Jones','Joe','J','10 Ave','New York','NY','11111','1111','111', '111-1111' 1 row created.

SQL> SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4, ) 2 values(2,'Smith','Sue','J','20 Ave','New York','NY','22222','2222','222', '222-111', 1 row created.

SQL> SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4, ) 2 values(3,'Anderson','Peggy','J','500 St','New York','NY','33333','3333','333', '3331 row created.

SQL> SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4, ) 2 values(4,'Andy','Jill', null,'930 St','New York','NY','44444','4444','212', '634-773 1 row created.

SQL> SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4, ) 2 values(5,'OK','Carl','L','19 Drive','New York','NY','55555','3234','212', '243-4243' 1 row created.

SQL> SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4, ) 2 values(6,'Peter','Jee','Q','38 Ave','New York','NY','66666','4598','212', '454-5443' 1 row created.

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

1 row created.

SQL> SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4, ) 2 values(8,'Young','Steve','J','388 Ave','New York','NY','88888','3468','212', '456-45 1 row created.

SQL> SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4, ) 2 values(9,'Mona','Joe','T','9300 Ave','Kansas City','MO','99999','3658','415', '456-4 1 row created.

SQL> SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4, ) 2 values(10,'Hackett','Karen','S','Kings Rd. Apt 833','Bellmore','NY','61202','3898',' 1 row created.

SQL> SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4, ) 2 values(11,'Bob','Jack','S','12 Giant Rd.','Newark','NJ','27377','3298','908', '123-7 1 row created. SQL> SQL> 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Andy 4 DECLARE Type cust_rec_t is RECORD (empl_no number, lastname varchar2(25), firstname varchar2(25) ); Type cust_tab_t is table of cust_rec_t index by binary_integer; cust_tab cust_tab_t; BEGIN UPDATE employee SET area_code = '917' WHERE area_code = '212' RETURNING empl_no, lastname, firstname BULK COLLECT INTO cust_tab; dbms_output.put_line(cust_tab(1).lastname ); dbms_output.put_line(cust_tab.COUNT ); END; /

PL/SQL procedure successfully completed. SQL> SQL> SQL>

SQL> SQL> drop table employee; Table dropped.

22. 17. 6. Demonstrate how to use DELETE with bulk processing.


SQL> SQL> SQL> SQL> create table employee_history 2 ( 3 empl_no integer 4 ,lastname varchar2(20) 5 ,firstname varchar2(15) 6 ,midinit varchar2(1) 7 ,street varchar2(30) 8 ,city varchar2(20) 9 ,state varchar2(2) 10 ,zip varchar2(5) 11 ,zip_4 varchar2(4) 12 ,area_code varchar2(3) 13 ,phone varchar2(8) 14 ,company_name varchar2(50) 15 ); Table created.

primary key not null not null

SQL> SQL> SQL> SQL> insert into employee_history(empl_no,lastname,firstname,midinit,street,city,state,zi 2 values(9,'OK','Joe','T','9 Ave','Kansas City','MO','65602','3658','415', '456-4563', 1 row created.

SQL> SQL> insert into employee_history(empl_no,lastname,firstname,midinit,street,city,state,zi 2 values(12,'Yes','Larry','T','1 Rd.','Newarkville','NJ','27377','3298','908', '123-73 1 row created.

SQL> SQL> insert into employee_history(empl_no,lastname,firstname,midinit,street,city,state,zi 2 values(13,'No','Cindy','T','13 Street','Warwick','RI','07377','3298','401', '123-738 1 row created.

SQL> SQL> insert into employee_history(empl_no,lastname,firstname,midinit,street,city,state,zi 2 values(14,'Fine','Doris','R','11th Ave','New York','NY','22299','3222','212', '123-1 1 row created.

SQL> SQL> SQL> SQL> create table ord( 2 order_no 3 ,empl_no 4 ,order_date 5 ,total_order_price 6 ,deliver_date 7 ,deliver_time 8 ,payment_method 9 ,emp_no 10 ,deliver_name 11 ,gift_message 12 ); Table created.

integer integer date not null number(7,2) date varchar2(7) varchar2(2) number(3,0) varchar2(35) varchar2(100)

primary key

SQL> SQL> SQL> insert into ord(order_no,empl_no,order_date,total_order_price,deliver_date,deliver_t 2 values(1,1,'14-Feb-2002', 23.00, '14-Feb-2002', '12 noon', 'CA',1, null, 'G 1 row created.

SQL> insert into ord(order_no ,empl_no ,order_date ,total_order_price ,deliver_date ,del 2 values(2,1,'14-Feb-2003', 510.98, '14-feb-2003', '5 pm', 'NY',7, 'Rose Ted' 1 row created.

SQL> insert into ord(order_no ,empl_no ,order_date ,total_order_price ,deliver_date ,del 2 values(3, 2,'14-Feb-2004', 315.99, '14-feb-2004', '3 pm', 'VS',2, 'Ani Fore 1 row created.

SQL> insert into ord(order_no ,empl_no ,order_date ,total_order_price ,deliver_date ,del 2 values(4, 2,'14-Feb-1999', 191.95, '14-feb-1999', '2 pm', 'NJ',2, 'O. John' 1 row created.

SQL> insert into ord(order_no ,empl_no ,order_date ,total_order_price ,deliver_date ,del 2 values(5, 6,'4-mar-2002', 101.95, '5-mar-2002', '2:30 pm', 'MO' , 2, 'Co 1 row created.

SQL> insert into ord(order_no ,empl_no ,order_date ,total_order_price ,deliver_date ,del 2 values(6, 9,'7-apr-2003', 221.95, '7-apr-2003', '3 pm', 'MA', 2, 'Sake Keit 1 row created.

SQL> insert into ord(order_no ,empl_no ,order_date ,total_order_price ,deliver_date ,del 2 values(7, 9,'20-jun-2004', 315.95, '21-jun-2004', '12 noon', 'BC', 2, 'Jess 1 row created.

SQL> insert into ord(order_no ,empl_no ,order_date ,total_order_price ,deliver_date ,del 2 values (8, 12, '31-dec-1999', 135.95, '1-jan-2000', '12 noon', 'DI', 3, ' 1 row created.

SQL> insert into ord(order_no ,empl_no ,order_date ,total_order_price ,deliver_date ,del 2 values (9, 12, '26-dec-2003', 715.95, '2-jan-2004', '12 noon', 'SK',7, 'Did 1 row created.

SQL> insert into ord(order_no ,empl_no ,order_date ,total_order_price ,deliver_date ,del 2 values(10, 4, sysdate-1, 119.95, sysdate+2, '6:30 pm', 'VG',2, 'P. Jing', ' 1 row created.

SQL> insert into ord(order_no ,empl_no ,order_date ,total_order_price ,deliver_date ,del 2 values(11, 2, sysdate, 310.00, sysdate+2, '3:30 pm', 'DC',2, 'C. Late', 'Ha 1 row created.

SQL> insert into ord(order_no ,empl_no ,order_date ,total_order_price ,deliver_date ,del 2 values(12, 7, sysdate-3, 121.95, sysdate-2, '1:30 pm', 'AC',2, 'W. Last', ' 1 row created.

SQL> insert into ord(order_no ,empl_no ,order_date ,total_order_price ,deliver_date ,del 2 values(13, 7, sysdate, 211.95, sysdate-4, '4:30 pm', 'CA',2, 'J. Bond', 'Th 1 row created. SQL> SQL> SQL> SQL> SQL> 2 3 4 5 6 7 8 9 10 11 12 13 14 2 2 0 0

-- First, review row-at-a-time processing: declare type cust_array_type is table of number index by binary_integer; employee_array cust_array_type; begin select empl_no bulk collect into employee_array from employee_history; FOR i IN employee_array.FIRST..employee_array.LAST LOOP delete from ord where empl_no = employee_array(i); dbms_output.put_line(sql%rowcount); END LOOP; end; /

PL/SQL procedure successfully completed.

SQL> SQL> SQL> drop table employee_history; Table dropped. SQL> SQL> drop table ord;

22. 17. 7. Bulk processing with the FORALL statement


SQL> SQL> SQL> SQL> create table employee_history 2 ( 3 empl_no integer 4 ,lastname varchar2(20) 5 ,firstname varchar2(15) 6 ,midinit varchar2(1) 7 ,street varchar2(30) 8 ,city varchar2(20) 9 ,state varchar2(2) 10 ,zip varchar2(5) 11 ,zip_4 varchar2(4) 12 ,area_code varchar2(3) 13 ,phone varchar2(8) 14 ,company_name varchar2(50) 15 ); Table created.

primary key not null not null

SQL> SQL> SQL> SQL> insert into employee_history(empl_no,lastname,firstname,midinit,street,city,state,zi 2 values(9,'OK','Joe','T','9 Ave','Kansas City','MO','65602','3658','415', '456-4563', 1 row created.

SQL> SQL> insert into employee_history(empl_no,lastname,firstname,midinit,street,city,state,zi 2 values(12,'Yes','Larry','T','1 Rd.','Newarkville','NJ','27377','3298','908', '123-73 1 row created.

SQL> SQL> insert into employee_history(empl_no,lastname,firstname,midinit,street,city,state,zi 2 values(13,'No','Cindy','T','13 Street','Warwick','RI','07377','3298','401', '123-738 1 row created.

SQL> SQL> insert into employee_history(empl_no,lastname,firstname,midinit,street,city,state,zi 2 values(14,'Fine','Doris','R','11th Ave','New York','NY','22299','3222','212', '123-1 1 row created. SQL> SQL> SQL> SQL> create table ord( 2 order_no 3 ,empl_no 4 ,order_date 5 ,total_order_price 6 ,deliver_date 7 ,deliver_time 8 ,payment_method 9 ,emp_no 10 ,deliver_name 11 ,gift_message 12 ); Table created.

integer integer date not null number(7,2) date varchar2(7) varchar2(2) number(3,0) varchar2(35) varchar2(100)

primary key

SQL> SQL> SQL> insert into ord(order_no,empl_no,order_date,total_order_price,deliver_date,deliver_t 2 values(1,1,'14-Feb-2002', 23.00, '14-Feb-2002', '12 noon', 'CA',1, null, 'G 1 row created.

SQL> insert into ord(order_no ,empl_no ,order_date ,total_order_price ,deliver_date ,del 2 values(2,1,'14-Feb-2003', 510.98, '14-feb-2003', '5 pm', 'NY',7, 'Rose Ted' 1 row created.

SQL> insert into ord(order_no ,empl_no ,order_date ,total_order_price ,deliver_date ,del 2 values(3, 2,'14-Feb-2004', 315.99, '14-feb-2004', '3 pm', 'VS',2, 'Ani Fore 1 row created.

SQL> insert into ord(order_no ,empl_no ,order_date ,total_order_price ,deliver_date ,del 2 values(4, 2,'14-Feb-1999', 191.95, '14-feb-1999', '2 pm', 'NJ',2, 'O. John' 1 row created.

SQL> insert into ord(order_no ,empl_no ,order_date ,total_order_price ,deliver_date ,del 2 values(5, 6,'4-mar-2002', 101.95, '5-mar-2002', '2:30 pm', 'MO' , 2, 'Co 1 row created.

SQL> insert into ord(order_no ,empl_no ,order_date ,total_order_price ,deliver_date ,del 2 values(6, 9,'7-apr-2003', 221.95, '7-apr-2003', '3 pm', 'MA', 2, 'Sake Keit 1 row created.

SQL> insert into ord(order_no ,empl_no ,order_date ,total_order_price ,deliver_date ,del 2 values(7, 9,'20-jun-2004', 315.95, '21-jun-2004', '12 noon', 'BC', 2, 'Jess 1 row created.

SQL> insert into ord(order_no ,empl_no ,order_date ,total_order_price ,deliver_date ,del 2 values (8, 12, '31-dec-1999', 135.95, '1-jan-2000', '12 noon', 'DI', 3, ' 1 row created.

SQL> insert into ord(order_no ,empl_no ,order_date ,total_order_price ,deliver_date ,del 2 values (9, 12, '26-dec-2003', 715.95, '2-jan-2004', '12 noon', 'SK',7, 'Did 1 row created.

SQL> insert into ord(order_no ,empl_no ,order_date ,total_order_price ,deliver_date ,del 2 values(10, 4, sysdate-1, 119.95, sysdate+2, '6:30 pm', 'VG',2, 'P. Jing', ' 1 row created.

SQL> insert into ord(order_no ,empl_no ,order_date ,total_order_price ,deliver_date ,del 2 values(11, 2, sysdate, 310.00, sysdate+2, '3:30 pm', 'DC',2, 'C. Late', 'Ha 1 row created.

SQL> insert into ord(order_no ,empl_no ,order_date ,total_order_price ,deliver_date ,del 2 values(12, 7, sysdate-3, 121.95, sysdate-2, '1:30 pm', 'AC',2, 'W. Last', ' 1 row created.

SQL> insert into ord(order_no ,empl_no ,order_date ,total_order_price ,deliver_date ,del 2 values(13, 7, sysdate, 211.95, sysdate-4, '4:30 pm', 'CA',2, 'J. Bond', 'Th 1 row created. SQL> SQL> SQL> SQL> declare 2 type cust_array_type is table of number 3 index by binary_integer; 4 employee_array cust_array_type; 5 v_index number; 6 begin 7 select empl_no bulk collect 8 into employee_array from employee_history; 9 10 FORALL i IN employee_array.FIRST..employee_array.LAST 11 delete from ord where empl_no = employee_array(i); 12 13 v_index := employee_array.FIRST; 14 for i in employee_array.FIRST..employee_array.LAST loop 15 dbms_output.put_line('delete for employee ' 16 ||employee_array(v_index)

17 ||' deleted ' 18 ||SQL%BULK_ROWCOUNT(v_index) 19 ||' rows.'); 20 v_index := employee_Array.NEXT(v_index); 21 end loop; 22 end; 23 / delete for employee 9 deleted 2 rows. delete for employee 12 deleted 2 rows. delete for employee 13 deleted 0 rows. delete for employee 14 deleted 0 rows. PL/SQL procedure successfully completed. SQL> SQL> drop table ord; Table dropped. SQL> drop table employee_history; Table dropped.

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

SQL> SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4,area_cod 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_cod 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_cod 2 values(3,'Anderson','Peggy','J','500 St','New York','NY','33333','3333','333', '333-3333','C Compa 1 row created.

SQL> SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4,area_cod 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_cod 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_cod 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_cod 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_cod 2 values(8,'Young','Steve','J','388 Ave','New York','NY','88888','3468','212', '456-4566','H Associa 1 row created.

SQL> SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4,area_cod 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_cod 2 values(10,'Hackett','Karen','S','Kings Rd. Apt 833','Bellmore','NY','61202','3898','516', '767-5677 1 row created.

SQL> SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4,area_cod 2 values(11,'Bob','Jack','S','12 Giant Rd.','Newark','NJ','27377','3298','908', '123-7367','Z Associate 1 row created. SQL> SQL> SQL> SQL> declare 2 type number_array is table of number; 3 type varchar2_array is table of varchar2(20); 4 v1 number_array; 5 v2 varchar2_array; 6 7 begin 8 9 select empl_no, lastname 10 bulk collect into v1, v2 11 from employee; 12 13 for i in 1..v1.LAST loop /* could use COUNT or SQL%rowcount */ 14 dbms_output.put_line(v2(i) ); 15 end loop; 16 end; 17 / Jones Smith

Anderson Andy OK Peter Baker Young Mona Hackett Bob PL/SQL procedure successfully completed. SQL> SQL> drop table employee; Table dropped.

22. 17. 9. Bulk collect from cursor with LIMIT option


SQL> SQL> create table employee 2 ( 3 empl_no 4 ,lastname 5 ,firstname 6 ,midinit 7 ,street 8 ,city 9 ,state 10 ,zip 11 ,zip_4 12 ,area_code 13 ,phone 14 ,company_name Table created.

integer varchar2(20) varchar2(15) varchar2(1) varchar2(30) varchar2(20) varchar2(2) varchar2(5) varchar2(4) varchar2(3) varchar2(8) varchar2(50));

primary key not null not null

SQL> SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4, 2 values(1,'Jones','Joe','J','10 Ave','New York','NY','11111','1111','111', '111-1111' 1 row created.

SQL> SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4, 2 values(2,'Smith','Sue','J','20 Ave','New York','NY','22222','2222','222', '222-111', 1 row created.

SQL> SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4, 2 values(3,'Anderson','Peggy','J','500 St','New York','NY','33333','3333','333', '3331 row created.

SQL> SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4, 2 values(4,'Andy','Jill', null,'930 St','New York','NY','44444','4444','212', '634-773 1 row created.

SQL> SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4, 2 values(5,'OK','Carl','L','19 Drive','New York','NY','55555','3234','212', '243-4243' 1 row created.

SQL> SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4, 2 values(6,'Peter','Jee','Q','38 Ave','New York','NY','66666','4598','212', '454-5443' 1 row created.

SQL> SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4, 2 values(7,'Baker','Paul','V','738 St.','Queens','NY','77777','3842','718', '664-4333' 1 row created.

SQL> SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4, 2 values(8,'Young','Steve','J','388 Ave','New York','NY','88888','3468','212', '456-45 1 row created.

SQL> SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4, 2 values(9,'Mona','Joe','T','9300 Ave','Kansas City','MO','99999','3658','415', '456-4 1 row created.

SQL> SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4, 2 values(10,'Hackett','Karen','S','Kings Rd. Apt 833','Bellmore','NY','61202','3898',' 1 row created.

SQL> SQL> insert into employee(empl_no,lastname,firstname,midinit,street,city,state,zip,zip_4, 2 values(11,'Bob','Jack','S','12 Giant Rd.','Newark','NJ','27377','3298','908', '123-7 1 row created.

SQL> SQL> declare 2 type employee_record_type is table of employee%rowtype; 3 cust_array employee_record_type; 4 cursor c is select * from employee order by 1; 5 begin 6 open c; 7 loop 8 fetch c bulk collect into cust_array LIMIT 7; 9 for i in 1..cust_array.COUNT loop 10 dbms_output.put_line(cust_array(i).lastname ); 11 end loop; 12 exit when c%notfound; 13 end loop; 14 close c; 15 end; 16 / Jones Smith Anderson Andy OK Peter Baker Young Mona Hackett Bob PL/SQL procedure successfully completed. SQL> SQL> SQL> drop table employee; Table dropped.

Assigning Index using bulk collect


When populating pl/sql table using bulk collect, assign index value using a column from the sql statement. For example:

select itemID, desc bulk collect into l_itemID, l_plsql_table[itemID].desc from items;

Right now to do this we've to loop thru the data and set the index.
Posted by Uday K at 01:08 AM | Add your thoughts (2)

April 06, 2005

Allow RETURNING for INSERT....SELECT


Allow RETURNING for INSERT....SELECT and similar SQL statements: FORALL INSERT RETURNING ... BULK COLLECT INTO ....
Posted by gary myers at 03:33 AM | Add your thoughts (2)

INTO

...

Incremental bulk processing


There should be a provision to incrementally bulk collect the query columns in a select statement into a plsql collection.

for e.g.
select from emp increment 100; emp_tab should be populated with emp in increments of 100. *

Currently there is a limit clause. But you have to keep two plsql tables, one to bulk collect and another in which the results a
Consider A Declare tbl_emp emp_tbl Begin Select < End; Now, Declare tbl_emp emp_tbl1 emp_tbl2 cursor Begin open loop fetch /** for loop emp_tbl2.count end exit +1 append c the i bulk c is is table it is not advisable to bulk collect nearly 50000 rows at one * Do bulk the is table a table with nearly 5 common million rows. In case

stretc

end < End; in order to populate the plsql table, we need to temporarily have one plsql table , which will Do the

If we have a syntax to incrementally bulk collect as per the limit (say 200), then all these things will be done und --> Emp_tbl will Select be populated * in batches bulk of 200.

Posted by viswanathan venkat ramanan at 08:44 AM | Add your thoughts (0)

April 12, 2005

Add ERROR_MESSAGE to SQL%BULK_EXCEPTIONS


Posted by sf at 07:44 AM | Add your thoughts (0)

Right now this pseudo-collection has ERROR_INDEX and ERROR_CODE fields, which is great. The problem is that we lose the

June 30, 2005

Returning * from DELETE


You can now write:

SELECT * BULK COLLECT INTO vLst FROM MyTable;


It is strange that you cannot write:

DELETE FROM MyTable RETURNING * BULK COLLECT INTO vLst;


It seems that RETURNING * is not supported. Instead, I have to write RETURNING col1, col2...

Das könnte Ihnen auch gefallen