declare @BatchSize int = 10000;while @BatchSize <> 0begin begin try begin transaction delete top (@BatchSize) from dbo.SPTLog output deleted.LogID, deleted.LogTime, getdate() into dbo.SPTLogArchive(LogID,LogTime,ArchiveTime) where LogTime < @dCutOffDate; set @BatchSize = @@ROWCOUNT; commit transaction; end try begin catch rollback transaction; set @BatchSize = 0; -- if you want to abort the delete, or set to 10000 if to continue end catchend
SELECT [LogID] ,[LogTime] ,GetDate()FROM [dbo].[SPTLog]WHERE [LogTime] < @dCutOffDate;