Skip to content

Instantly share code, notes, and snippets.

@tgh0831
Last active November 18, 2016 13:37
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 tgh0831/ab4709155fa168c12231f2c69b6617e5 to your computer and use it in GitHub Desktop.
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.
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