SQL Server - Delete Large table data with Where clause

If we want to delete data from large table with some where caluse, the best way to do is :

Select COUNT(*)  FROM [Server].[Database].[MyTable] WHERE ...

By doing above we can know the count of records to be deleted.

DECLARE @RowsDeleted INTEGER
DECLARE @RowsToDeleteAtATime
SET @RowsDeleted = 1
SET @RowsToDeleteAtATime = 50000

WHILE (@RowsDeleted > 0)
    BEGIN
        DELETE TOP (@RowsToDeleteAtATime) FROM [Server].[Database].[MyTable] WHERE ...
        SET @RowsDeleted = @@ROWCOUNT
    END

And by doing :

Select COUNT(*)  FROM [Server].[Database].[MyTable] WHERE ...

We can know the status.

Comments

Popular posts from this blog

public vs protected vs default access modifiers - Java

Class, Reference and Object