I have a pl sql procedure which accepts array of elements and inserts them into a table.
At the beginning of the procedure i am deleting data from backup table and inserting data from main table into backup table. Then i am deleting data from main table and looping through the arguments to the proc and inserting records. When i face dup_val_on_index exception
, the rollback is happening to start point of the proc. I mean the exception block is getting executed. But the rollback is not happening.
For example, if i insert 2 rows which has duplicate values, dup_val_on_index exception
has to be raised and 1st row should not be inserted.
Below is my code. If any exception happening inside the loop, i want to rollback the insert as well and delete operation performed at the beginning of the procedure
PROCEDURE insert_sales_data (
p_depot_code IN depotcode_array,
p_depot_name IN depotname_array,
p_dell_split IN dellsplit_array,
p_sector IN sector_array,
p_locality IN locality_array,
p_tnt_depot_code IN tntdepotcode_array,
p_postal_code IN postalcode_array,
p_primary_sort IN primarysort_array,
p_secondary_sort IN secondarysort_array,
p_user IN VARCHAR2,
p_error_message OUT VARCHAR2,
p_count OUT NUMBER
)
IS
BEGIN
SAVEPOINT s1;
DELETE FROM sales_backup;
INSERT INTO sales_backup
SELECT
*
FROM
sales;
DELETE FROM sales;
FOR i IN p_sector.first..p_sector.last LOOP
BEGIN
INSERT INTO sales (
depot_code,
depot_name,
dell_split,
sector,
locality,
tnt_depot_code,
postal_code,
primary_sort,
secondary_sort,
create_date,
create_user_id,
uuid
) VALUES (
p_depot_code(i),
p_depot_name(i),
p_dell_split(i),
p_sector(i),
p_locality(i),
p_tnt_depot_code(i),
p_postal_code(i),
p_primary_sort(i),
p_secondary_sort(i),
SYSDATE,
p_user,
sys_guid()
);
EXCEPTION
WHEN dup_val_on_index THEN
ROLLBACK TO s1;
EXIT;
WHEN OTHERS THEN
ROLLBACK TO s1;
EXIT;
END;
END LOOP;
SELECT
COUNT(*)
INTO p_count
FROM
uk_depots;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK TO s1;
END;