PLSQL PT Bulk Binding
PLSQL PT Bulk Binding
PLSQL PT Bulk Binding
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
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;
end;
declare
lv_nest_tab tt_nest_tab_type:=tt_nest_tab_type();
lv_postal_code_rec postal_code%rowtype;
begin
end;
/
Declare
Begin
Forall I in lv_num_list.FIRST..lv_num_list.LAST
End;
Declare
Begin
End;
Declare
Begin
Write a PLSQL Block to load all employee names from “EMP” table into variable of type “NESTED
TABLE”
Version 1:
Declare
Begin
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);
Begin
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
Begin
Open c1;
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;
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;
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;