Tuesday, 4 November 2025

Batch Deleting Millions of Records using SET ROWCOUNT Statement

While we are deleting the records form a big table which have billion or million of records. Instead of deleting the records at one time we are deleting the records in batch.

Below is the script to delete the records in a batch.

-- Process 10,000 rows at a time

SET ROWCOUNT 10000;

 WHILE 1 = 1

BEGIN

    DELETE FROM Table_name

    WHERE condtion;

     IF @@ROWCOUNT = 0

        BREAK; -- No more rows left to delete

END

 -- Reset rowcount to normal

SET ROWCOUNT 0;

No comments:

Post a Comment

If you have any doubt, please let me know.

Popular Posts