Last active
November 18, 2016 13:37
-
-
Save tgh0831/ab4709155fa168c12231f2c69b6617e5 to your computer and use it in GitHub Desktop.
T-SQL script to delete a whole bunch of records from a table without growing the log. In this example my table has a field called "recordinsertdt" and I'm keeping the most recent 215 days and deleting the rest. Records are deleted 50000 rows at a time.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
doOver: | |
DELETE TOP (50000) | |
FROM mytable | |
WHERE recordinsertdt <= | |
( | |
SELECT DATEADD(day, -215, CAST(GETDATE() AS DATE)) | |
); | |
IF @@rowcount > 0 | |
GOTO doOver; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment