You are on page 1of 2

Forma de eliminar múltiples registros en Oracle

CREATE OR REPLACE PROCEDURE delete_rows_in_batches AS

v_batch_size CONSTANT NUMBER := 1000; -- Set the batch size (number of rows to delete
per batch)

v_total_rows NUMBER;

BEGIN

-- Get the total number of rows to be deleted

SELECT COUNT(*) INTO v_total_rows FROM your_table WHERE your_condition;

-- Loop through and delete rows in batches

FOR i IN 1..CEIL(v_total_rows / v_batch_size) LOOP

-- Delete rows in the current batch

DELETE FROM your_table

WHERE your_condition

AND ROWNUM <= v_batch_size; -- Delete up to 'v_batch_size' rows

-- Commit after each batch to release locks and manage transaction size

COMMIT;

-- Pause for a moment to allow other processes to execute (optional)

DBMS_LOCK.SLEEP(1); -- Sleep for 1 second (optional)

END LOOP;

-- Optional: Commit final changes after all rows are deleted

COMMIT;

-- Display completion message

DBMS_OUTPUT.PUT_LINE('Deletion process completed successfully.');

EXCEPTION

WHEN OTHERS THEN


-- Rollback changes and handle exceptions

ROLLBACK;

DBMS_OUTPUT.PUT_LINE('Error occurred: ' || SQLERRM);

END delete_rows_in_batches;

You might also like