Last active
November 15, 2018 11:05
-
-
Save kr153/409bb3e4fc021094c7782950c6994ebd to your computer and use it in GitHub Desktop.
Clear ErrorLogs Table in Loop
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
| --kr153 | |
| SET NOCOUNT ON | |
| DECLARE @BATCHCOUNT INT | |
| --------------------------------------- | |
| /* BATCH COUNT - CHANGE AS NEEDED */ | |
| --------------------------------------- | |
| SET @BATCHCOUNT = 1000 | |
| --------------------------------------- | |
| DECLARE @ROWS INT | |
| DECLARE @TOTAL BIGINT | |
| DECLARE @REMAINING BIGINT | |
| SET @ROWS = 1 | |
| -- 'month' keyword below can be changed to 'day' or 'year' if needed | |
| -------------------------------------------------------------------------------------------- | |
| /* QUERY - CHANGE AS NEEDED */ | |
| -------------------------------------------------------------------------------------------- | |
| /* select all except last 1 month */ SELECT @TOTAL = COUNT (*) FROM ERRORLOGS WITH(NOLOCK) WHERE ERRORTIME < DATEADD(month, -1, GETDATE()); | |
| --> SAMPLE FOR 2 months -- SELECT @TOTAL = COUNT (*) FROM ERRORLOGS WITH(NOLOCK) WHERE ERRORTIME < DATEADD(month, -2, GETDATE()); | |
| --> SAMPLE FOR 3 months -- SELECT @TOTAL = COUNT (*) FROM ERRORLOGS WITH(NOLOCK) WHERE ERRORTIME < DATEADD(month, -3, GETDATE()); | |
| -------------------------------------------------------------------------------------------- | |
| SET @REMAINING = @TOTAL | |
| WHILE (@ROWS > 0) | |
| BEGIN | |
| ----------------------------------------------------------------------------- | |
| /* QUERY - CHANGE AS NEEDED */ | |
| ----------------------------------------------------------------------------- | |
| DELETE | |
| TOP (@BATCHCOUNT) | |
| FROM | |
| ERRORLOGS | |
| WHERE | |
| /* delete all except last 1 month */ ERRORTIME < DATEADD(month, -1, GETDATE()) | |
| --> SAMPLE FOR 2 months -- ERRORTIME < DATEADD(month, -2, GETDATE()) | |
| --> SAMPLE FOR 3 months -- ERRORTIME < DATEADD(month, -3, GETDATE()) | |
| ----------------------------------------------------------------------------- | |
| SET @ROWS = @@ROWCOUNT | |
| SET @REMAINING = @REMAINING - @ROWS | |
| PRINT 'Remaining ' + CAST(@REMAINING AS VARCHAR) + '/' + CAST(@TOTAL AS VARCHAR) + ' Rows.' | |
| END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment