I use the following stored procedure to delete a large number of records. I understand that the DELETE statement writes to the transaction log and deletes many lines, which will increase the log.
I considered other options for creating tables and inserting records to save and then crop the source, this method will not work for me.
How to make my stored procedure below more efficient by making sure I keep the transaction log unnecessary?
CREATE PROCEDURE [dbo].[ClearLog] ( @Age int = 30 ) AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- DELETE ERRORLOG WHILE EXISTS ( SELECT [LogId] FROM [dbo].[Error_Log] WHERE DATEDIFF( dd, [TimeStamp], GETDATE() ) > @Age ) BEGIN SET ROWCOUNT 10000 DELETE [dbo].[Error_Log] WHERE DATEDIFF( dd, [TimeStamp], GETDATE() ) > @Age WAITFOR DELAY '00:00:01' SET ROWCOUNT 0 END END
source share