Skip to content

Instantly share code, notes, and snippets.

@syron
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save syron/ec39e575496e30d2c185 to your computer and use it in GitHub Desktop.
Save syron/ec39e575496e30d2c185 to your computer and use it in GitHub Desktop.
SQL loop to delete x rows at a time.
DECLARE @rowCount INT
, @rowsToDelete INT = 5000
, @highestDate DATETIME2(7) = '2015-03-01'
WHILE 1 = 1
BEGIN
DELETE TOP (@rowsToDelete) T FROM Table T
WHERE T.DateTimeColumn < @highestDate
SET @rowCount = @@ROWCOUNT
IF @rowCount <> @rowsToDelete
BREAK
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment