PLSQL PT Bulk Binding

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

PLSQL PT

Bulk Binding

In-bind : When an insert,update or merge statement stores a plsql or host variable in the DB

Out-Bind: When the RETURNING INTO clause of an insert,update or delete statement assigns a
database value to a plsql or host variable(FORALL Statement)

DEFINE: When a Select or Fetch statement assigns a database value to a plsql or host
variable(Bulk Collect)

In-Bind Example: Using For All Statement :To reduce the number of context switches

declare

type tt_nest_tab_type is table of postal_code%rowtype;

lv_nest_tab tt_nest_tab_type:=tt_nest_tab_type();

lv_postal_code_rec postal_code%rowtype;

begin
for i in (select * from postal_code)

loop

lv_postal_code_rec.postoffice_name:=i.postoffice_name;

lv_postal_code_rec.pincode := i.pincode;

lv_postal_code_rec.district_name := i.district_name;

lv_postal_code_rec.city:=i.city;

lv_postal_code_rec.state :=i.state;

lv_nest_tab.extend();

lv_nest_tab(lv_nest_tab.last) := lv_postal_code_rec;

end loop;

dbms_output.put_line('lv_nest_tab.count =' ||lv_nest_tab.count);

end;

declare

type tt_nest_tab_type is table of postal_code%rowtype;

lv_nest_tab tt_nest_tab_type:=tt_nest_tab_type();

lv_postal_code_rec postal_code%rowtype;

begin

select * bulk collect into lv_nest_tab from postal_code;

dbms_output.put_line('lv_nest_tab.count =' ||lv_nest_tab.count);

end;

/
Declare

Type t_num_list_type is table of number;

lv_num_list t_num_list_type := t_num_list_type(1,2,3,4,5);

Begin

Forall I in lv_num_list.FIRST..lv_num_list.LAST

Insert into a values(lv_num_list(i));

End;

Out-Bind: Returning Clause

Declare

Type t_num_list_type is table of number;

Lv_num_list t_num_list_type := t_num_list_type();

Begin

Update A set col=col+10 returning col bulk collect into lv_num_list;

End;

Define Clause: Bulk Collect

Declare

Type t_num_list_type is table of number;

Lv_num_list t_num_list_type := t_num_list_type();

Begin

Select col bulk collect into lv_num_list from A;


End;

Write a PLSQL Block to load all employee names from “EMP” table into variable of type “NESTED
TABLE”

Version 1:

Declare

Type emp_name_list is table of varchar2(100);

Lv_emp_name_list emp_name_list :=emp_name_list();

Begin

For I in (select * from emp)

Loop

Lv_emp_name_list.extend;

Lv_emp_name_list(lv_emp_name_list.last):=i.ename;

End loop;

End;

Version 2:

Declare

Type emp_name_list is

Table of varchar2(100);

Lv_emp_name emp_name_list :=emp_name_list();

Begin

For I in (select * from emp)

Loop

Lv_emp_name_list.extend;
Lv_emp_name_list(lv_emp_name_list.last):=i.ename;

End loop;

For I in lv_emp_name_list.first..lv_emp_name.last

Loop

Dbms_output.put_line(lv_emp_name_list(i));

End loop;

End;

End;

Declare

Type t_num_list_type is table of number;

Lv_num_list t_num_list_type := t_num_list_type();

Cursor c1 is select * from a;

Begin

Open c1;

Fetch c1 Bulk collect into lv_num_list;

Close c1;

End;
/

DECLARE
CURSOR student_cur IS
SELECT student_id, first_name, last_name
FROM student;
BEGIN
FOR rec IN student_cur LOOP
DBMS_OUTPUT.PUT_LINE ('student_id: '||rec.student_id);
DBMS_OUTPUT.PUT_LINE ('first_name: '||rec.first_name);
DBMS_OUTPUT.PUT_LINE ('last_name: '||rec.last_name);
END LOOP;
END;

DECLARE
-- Define collection type and variables to be used by the
-- BULK COLLECT clause
TYPE student_id_type IS TABLE OF student.student_id%TYPE;
TYPE first_name_type IS TABLE OF student.first_name%TYPE;
TYPE last_name_type IS TABLE OF student.last_name%TYPE;
student_id_tab student_id_type;
first_name_tab first_name_type;
last_name_tab last_name_type;
BEGIN
-- Fetch all student data at once via BULK COLLECT clause
SELECT student_id, first_name, last_name
BULK COLLECT INTO student_id_tab, first_name_tab, last_name_tab
FROM student;
FOR i IN student_id_tab.FIRST..student_id_tab.LAST
LOOP
DBMS_OUTPUT.PUT_LINE ('student_id: '||student_id_tab(i));
DBMS_OUTPUT.PUT_LINE ('first_name: '||first_name_tab(i));
DBMS_OUTPUT.PUT_LINE ('last_name: '||last_name_tab(i));
END LOOP;
END;

Usage of LIMIT Clause:

DECLARE
CURSOR student_cur IS
SELECT student_id, first_name, last_name
FROM student;
-- Define collection type and variables to be used by the
-- BULK COLLECT clause
TYPE student_id_type IS TABLE OF student.student_id%TYPE;
TYPE first_name_type IS TABLE OF student.first_name%TYPE;
TYPE last_name_type IS TABLE OF student.last_name%TYPE;
student_id_tab student_id_type;
first_name_tab first_name_type;
last_name_tab last_name_type;
-- Define variable to be used by the LIMIT clause
v_limit PLS_INTEGER := 50;
BEGIN
OPEN student_cur;
LOOP
-- Fetch 50 rows at once
FETCH student_cur
BULK COLLECT INTO student_id_tab, first_name_tab,
last_name_tab
LIMIT v_limit;
EXIT WHEN student_id_tab.COUNT = 0;
FOR i IN student_id_tab.FIRST..student_id_tab.LAST
LOOP
DBMS_OUTPUT.PUT_LINE ('student_id: '||student_id_tab(i));
DBMS_OUTPUT.PUT_LINE ('first_name: '||first_name_tab(i));
DBMS_OUTPUT.PUT_LINE ('last_name: '||last_name_tab(i));
END LOOP;
END LOOP;
CLOSE student_cur;
END;

DECLARE
CURSOR student_cur IS SELECT student_id, first_name, last_name FROM student;
-- Define record type
TYPE student_rec IS RECORD(student_id student.student_id%TYPE,first_name
student.first_name%TYPE,last_name student.last_name%TYPE);
-- Define collection type
TYPE student_type IS TABLE OF student_rec;
-- Define collection variable
student_tab student_type;
-- Define variable to be used by the LIMIT clause
v_limit PLS_INTEGER := 5;
BEGIN
OPEN student_cur;
LOOP
-- Fetch 50 rows at once
FETCH student_cur BULK COLLECT INTO student_tab LIMIT v_limit;
EXIT WHEN student_tab.COUNT = 0;
FOR i IN student_tab.FIRST..student_tab.LAST
LOOP
DBMS_OUTPUT.PUT_LINE
('student_id: '||student_tab(i).student_id);
DBMS_OUTPUT.PUT_LINE
('first_name: '|| student_tab(i).first_name);

DBMS_OUTPUT.PUT_LINE
('last_name: '|| student_tab(i).last_name);
END LOOP;
END LOOP;
CLOSE student_cur;
END;

DECLARE
-- Define collection types and variables
TYPE row_num_type IS TABLE OF NUMBER INDEX BY PLS_INTEGER;
TYPE row_text_type IS TABLE OF VARCHAR2(10) INDEX BY PLS_INTEGER;
row_num_tab row_num_type;
row_text_tab row_text_type;
BEGIN
DELETE FROM TEST
RETURNING row_num, row_text
BULK COLLECT INTO row_num_tab, row_text_tab;
DBMS_OUTPUT.PUT_LINE ('Deleted '||SQL%ROWCOUNT ||' rows:');
FOR i IN row_num_tab.FIRST..row_num_tab.LAST
LOOP
DBMS_OUTPUT.PUT_LINE ('row_num = '||row_num_tab(i)||
' row_text = ' ||row_text_tab(i));
END LOOP;
COMMIT;
END;

Select some data from the ZIPCODE table and inserts it into the
MY_ZIPCODE table

DECLARE
-- Declare collection types
TYPE string_type IS TABLE OF VARCHAR2(100) INDEX BY PLS_INTEGER;
TYPE date_type IS TABLE OF DATE INDEX BY PLS_INTEGER;
-- Declare collection variables to be used by the FORALL statement
zip_tab string_type;
city_tab string_type;
state_tab string_type;
cr_by_tab string_type;
cr_date_tab date_type;
mod_by_tab string_type;
mod_date_tab date_type;
v_counter PLS_INTEGER := 0;
v_total INTEGER := 0;
BEGIN
-- Populate individual collections
SELECT *
BULK COLLECT INTO zip_tab, city_tab, state_tab, cr_by_tab,
cr_date_tab, mod_by_tab, mod_date_tab
FROM zipcode
WHERE state = 'CT';
-- Populate MY_ZIPCODE table
FORALL i in 1..zip_tab.COUNT
INSERT INTO my_zipcode
(zip, city, state, created_by, created_date, modified_by,
modified_date)
VALUES
(zip_tab(i), city_tab(i), state_tab(i), cr_by_tab(i),
cr_date_tab(i), mod_by_tab(i), mod_date_tab(i));
COMMIT;

-- Check how many records were added to MY_ZIPCODE table

SELECT COUNT(*)
INTO v_total
FROM my_zipcode
WHERE state = 'CT';
DBMS_OUTPUT.PUT_LINE
(v_total||' records were added to MY_ZIPCODE table');
END;

CREATE TABLE my_instructor AS


SELECT *
FROM instructor;

SET SERVEROUTPUT ON;


DECLARE
-- Define collection types and variables to be used by the
-- BULK COLLECT clause
TYPE instructor_id_type IS TABLE OF
my_instructor.instructor_id%TYPE;
TYPE first_name_type IS TABLE OF my_instructor.first_name%TYPE;
TYPE last_name_type IS TABLE OF my_instructor.last_name%TYPE;
instructor_id_tab instructor_id_type;
first_name_tab first_name_type;
last_name_tab last_name_type;
BEGIN
-- Fetch all instructor data at once via BULK COLLECT clause
SELECT instructor_id, first_name, last_name
BULK COLLECT INTO instructor_id_tab, first_name_tab,
last_name_tab
FROM my_instructor;
FOR i IN instructor_id_tab.FIRST..instructor_id_tab.LAST
LOOP
DBMS_OUTPUT.PUT_LINE ('instructor_id: '||instructor_id_tab(i));
DBMS_OUTPUT.PUT_LINE ('first_name: '||first_name_tab(i));
DBMS_OUTPUT.PUT_LINE ('last_name: '||last_name_tab(i));
END LOOP;
END;
DECLARE
-- Define collection types and variables to be used by the
-- BULK COLLECT clause
TYPE instructor_id_type IS TABLE OF
my_instructor.instructor_id%TYPE;
TYPE first_name_type IS TABLE OF my_instructor.first_name%TYPE;
TYPE last_name_type IS TABLE OF my_instructor.last_name%TYPE;
instructor_id_tab instructor_id_type;
first_name_tab first_name_type;
last_name_tab last_name_type;
BEGIN
-- Fetch all instructor data at once via BULK COLLECT clause
SELECT instructor_id, first_name, last_name
BULK COLLECT INTO instructor_id_tab, first_name_tab,
last_name_tab
FROM my_instructor;
IF instructor_id_tab.COUNT > 0 THEN
FOR i IN instructor_id_tab.FIRST..instructor_id_tab.LAST
LOOP
DBMS_OUTPUT.PUT_LINE ('instructor_id:
'||instructor_id_tab(i));
DBMS_OUTPUT.PUT_LINE ('first_name: '||first_name_tab(i));
DBMS_OUTPUT.PUT_LINE ('last_name: '||last_name_tab(i));
END LOOP;
END IF;
END;

SET SERVEROUTPUT ON;


DECLARE
-- Define collection types and variables to be used by the
-- BULK COLLECT clause
TYPE instructor_id_type IS TABLE OF
my_instructor.instructor_id%TYPE;
TYPE first_name_type IS TABLE OF my_instructor.first_name%TYPE;
TYPE last_name_type IS TABLE OF my_instructor.last_name%TYPE;
instructor_id_tab instructor_id_type;
first_name_tab first_name_type;
last_name_tab last_name_type;
BEGIN
-- Fetch all instructor data at once via BULK COLLECT clause
SELECT instructor_id, first_name, last_name
BULK COLLECT INTO instructor_id_tab, first_name_tab,
last_name_tab
FROM my_instructor;
IF instructor_id_tab.COUNT > 0 THEN
FOR i IN instructor_id_tab.FIRST..instructor_id_tab.LAST
LOOP
DBMS_OUTPUT.PUT_LINE ('instructor_id:
'||instructor_id_tab(i));
DBMS_OUTPUT.PUT_LINE ('first_name: '||first_name_tab(i));
DBMS_OUTPUT.PUT_LINE ('last_name: '||last_name_tab(i));
END LOOP;
END IF;
END;

SET SERVEROUTPUT ON;


DECLARE
-- Define collection types and variables to be used by the
-- BULK COLLECT clause
TYPE instructor_id_type IS TABLE OF
my_instructor.instructor_id%TYPE;
TYPE first_name_type IS TABLE OF my_instructor.first_name%TYPE;
TYPE last_name_type IS TABLE OF my_instructor.last_name%TYPE;
instructor_id_tab instructor_id_type;
first_name_tab first_name_type;
last_name_tab last_name_type;
BEGIN
-- Fetch all instructor data at once via BULK COLLECT clause
SELECT instructor_id, first_name, last_name
BULK COLLECT INTO instructor_id_tab, first_name_tab,
last_name_tab
FROM my_instructor;
IF instructor_id_tab.COUNT > 0 THEN
FOR i IN instructor_id_tab.FIRST..instructor_id_tab.LAST
LOOP
DBMS_OUTPUT.PUT_LINE ('instructor_id:
'||instructor_id_tab(i));
DBMS_OUTPUT.PUT_LINE ('first_name: '||first_name_tab(i));
DBMS_OUTPUT.PUT_LINE ('last_name: '||last_name_tab(i));
END LOOP;
END IF;
END;

SET SERVEROUTPUT ON;


DECLARE
CURSOR instructor_cur IS
SELECT instructor_id, first_name, last_name
FROM my_instructor;
-- Define collection types and variables to be used by the
-- BULK COLLECT clause
TYPE instructor_id_type IS TABLE OF
my_instructor.instructor_id%TYPE;
TYPE first_name_type IS TABLE OF my_instructor.first_name%TYPE;
TYPE last_name_type IS TABLE OF my_instructor.last_name%TYPE;
instructor_id_tab instructor_id_type;
first_name_tab first_name_type;
last_name_tab last_name_type;
v_limit PLS_INTEGER := 5;
BEGIN
OPEN instructor_cur;
LOOP
-- Fetch partial instructor data at once via BULK COLLECT
-- clause
FETCH instructor_cur
BULK COLLECT INTO instructor_id_tab, first_name_tab,
last_name_tab
LIMIT v_limit;
EXIT WHEN instructor_id_tab.COUNT = 0;
FOR i IN instructor_id_tab.FIRST..instructor_id_tab.LAST
LOOP
DBMS_OUTPUT.PUT_LINE ('instructor_id:
'||instructor_id_tab(i));
DBMS_OUTPUT.PUT_LINE ('first_name: '||first_name_tab(i));
DBMS_OUTPUT.PUT_LINE ('last_name: '||last_name_tab(i));
END LOOP;
END LOOP;
CLOSE instructor_cur;
END;

SET SERVEROUTPUT ON;


DECLARE
CURSOR instructor_cur IS
SELECT instructor_id, first_name, last_name
FROM my_instructor;
-- Define record type
TYPE instructor_rec IS RECORD
(instructor_id my_instructor.instructor_id%TYPE,
first_name my_instructor.first_name%TYPE,
last_name my_instructor.last_name%TYPE);
-- Define collection type and variable to be used by the
-- BULK COLLECT clause
TYPE instructor_type IS TABLE OF instructor_rec;
instructor_tab instructor_type;
v_limit PLS_INTEGER := 5;
BEGIN
OPEN instructor_cur;
LOOP
-- Fetch partial instructor data at once via BULK COLLECT
-- clause
FETCH instructor_cur
BULK COLLECT INTO instructor_tab
LIMIT v_limit;
EXIT WHEN instructor_tab.COUNT = 0;
FOR i IN instructor_tab.FIRST..instructor_tab.LAST
LOOP
DBMS_OUTPUT.PUT_LINE
('instructor_id: '||instructor_tab(i).instructor_id);
DBMS_OUTPUT.PUT_LINE
('first_name: '||instructor_tab(i).first_name);
DBMS_OUTPUT.PUT_LINE
('last_name: '||instructor_tab(i).last_name);
END LOOP;
END LOOP;
CLOSE instructor_cur;
END;

SET SERVEROUTPUT ON;


DECLARE
CURSOR instructor_cur IS
SELECT instructor_id, first_name, last_name
FROM my_instructor;
-- Define collection type and variable to be used by the
-- BULK COLLECT clause
TYPE instructor_type IS TABLE OF instructor_cur%ROWTYPE;
instructor_tab instructor_type;
v_limit PLS_INTEGER := 5;
BEGIN
OPEN instructor_cur;
LOOP
-- Fetch partial instructor data at once via BULK COLLECT
-- clause
FETCH instructor_cur
BULK COLLECT INTO instructor_tab
LIMIT v_limit;
EXIT WHEN instructor_tab.COUNT = 0;
FOR i IN instructor_tab.FIRST..instructor_tab.LAST
LOOP
DBMS_OUTPUT.PUT_LINE
('instructor_id: '||instructor_tab(i).instructor_id);
DBMS_OUTPUT.PUT_LINE
('first_name: '||instructor_tab(i).first_name);
DBMS_OUTPUT.PUT_LINE
('last_name: '||instructor_tab(i).last_name);
END LOOP;
END LOOP;
CLOSE instructor_cur;
END;

SET SERVEROUTPUT ON;


DECLARE
-- Define collection types and variables to be used by the
-- BULK COLLECT clause
TYPE instructor_id_type IS TABLE OF
my_instructor.instructor_id%TYPE;
TYPE first_name_type IS TABLE OF my_instructor.first_name%TYPE;
TYPE last_name_type IS TABLE OF my_instructor.last_name%TYPE;
instructor_id_tab instructor_id_type;
first_name_tab first_name_type;
last_name_tab last_name_type;
BEGIN
DELETE FROM MY_INSTRUCTOR
RETURNING instructor_id, first_name, last_name
BULK COLLECT INTO instructor_id_tab, first_name_tab,
last_name_tab;
DBMS_OUTPUT.PUT_LINE ('Deleted '||SQL%ROWCOUNT||' rows ');
IF instructor_id_tab.COUNT > 0 THEN
FOR i IN instructor_id_tab.FIRST..instructor_id_tab.LAST
LOOP
DBMS_OUTPUT.PUT_LINE
('instructor_id: '||instructor_id_tab(i));
DBMS_OUTPUT.PUT_LINE ('first_name: '||first_name_tab(i));
DBMS_OUTPUT.PUT_LINE ('last_name: '||last_name_tab(i));
END LOOP;
END IF;
COMMIT;
END;

You might also like