Bulk Operation That Continues Despite Exceptions
DROP TABLE emp_temp CASCADE CONSTRAINTS PURGE;
-- create a temporary table for this example
CREATE TABLE emp_temp
AS
SELECT *
FROM employees;
DECLARE
TYPE empid_tab IS TABLE OF employees.employee_id%TYPE;
emp_sr EMPID_TAB;
-- create an exception handler for ORA-24381
errors NUMBER;
dml_errors EXCEPTION;
PRAGMA EXCEPTION_INIT(dml_errors,-24381);
BEGIN
SELECT employee_id
BULK COLLECT INTO emp_sr
FROM emp_temp
WHERE hire_date < '30-DEC-94';
-- add '_SR' to the job_id of the most senior employees
FORALL i IN emp_sr.FIRST..emp_sr.LAST SAVE EXCEPTIONS
UPDATE emp_temp
SET job_id = job_id
||'_SR'
WHERE Emp_sr(i) = emp_temp.employee_id;
-- If any errors occurred during the FORALL SAVE EXCEPTIONS,
-- a single exception is raised when the statement completes.
EXCEPTION
WHEN dml_errors THEN -- Now we figure out what failed and why.
errors := SQL%BULK_EXCEPTIONS.COUNT;
dbms_output.Put_line('Number of statements that failed: '
||errors);
FOR i IN 1.. errors LOOP
dbms_output.Put_line('Error #'
||i
||' occurred during '
||'iteration #'
||SQL%Bulk_exceptions(i).ERROR_INDEX);
dbms_output.Put_line('Error message is '
||Sqlerrm(-SQL%Bulk_exceptions(i).ERROR_CODE));
END LOOP;
END;
/
OUTPUT:-
Number of statements that failed: 2
Error #1 occurred during iteration #5
Error message is ORA-12899: value too large for column (actual: , maximum: )
Error #2 occurred during iteration #12
Error message is ORA-12899: value too large for column (actual: , maximum: )
PL/SQL procedure successfully completed.
0 comments:
Post a Comment