Using FORALL With BULK COLLECT
DROP TABLE emp_temp CASCADE CONSTRAINTS PURGE;
CREATE TABLE emp_temp
AS
SELECT *
FROM employees;
DECLARE
TYPE numlist IS TABLE OF NUMBER;
depts NUMLIST := Numlist(10,20,30);
TYPE enum_t IS TABLE OF employees.employee_id%TYPE;
TYPE dept_t IS TABLE OF employees.department_id%TYPE;
e_ids ENUM_T;
d_ids DEPT_T;
BEGIN
FORALL j IN depts.FIRST..depts.LAST
DELETE FROM emp_temp
WHERE department_id = Depts(j)
RETURNING employee_id,department_id BULK COLLECT INTO e_ids,d_ids;
dbms_output.Put_line('Deleted '
||SQL%ROWCOUNT
||' rows:');
FOR i IN e_ids.FIRST.. e_ids.LAST LOOP
dbms_output.Put_line('Employee #'
||E_ids(i)
||' from dept #'
||D_ids(i));
END LOOP;
END;
/
OUTPUT:-
Deleted 9 rows:
Employee #200 from dept #10
Employee #201 from dept #20
Employee #202 from dept #20
Employee #114 from dept #30
Employee #115 from dept #30
Employee #116 from dept #30
Employee #117 from dept #30
Employee #118 from dept #30
Employee #119 from dept #30
PL/SQL procedure successfully completed.
0 comments:
Post a Comment