sql SQL Server - 清除循环中的ErrorLogs表
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql SQL Server - 清除循环中的ErrorLogs表相关的知识,希望对你有一定的参考价值。
--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
以上是关于sql SQL Server - 清除循环中的ErrorLogs表的主要内容,如果未能解决你的问题,请参考以下文章
如何清除 SQL Server 查询缓存?
SQL Server 2008 中的截断/清除表变量
SQL Server 快速清除日志文件的方法
清除并填充SQL Server中的表
SQL Server 删除性能
SQL Server 自增长清除