Skip to content

Instantly share code, notes, and snippets.

@mikeplate
Last active July 28, 2022 07:09
Show Gist options
  • Save mikeplate/be2802b9ea14429b3c64f0f51456f507 to your computer and use it in GitHub Desktop.
Save mikeplate/be2802b9ea14429b3c64f0f51456f507 to your computer and use it in GitHub Desktop.
Run t-sql delete statement with adaptable rowcount for easier load on server until finished
declare @rowcount int = 1000
declare @maxseconds int = 10
declare @waitseconds int = 5
declare @sql nvarchar(max) = 'delete from Log where CreatedDate<''2022-06-28'' '
declare @message nvarchar(max)
declare @start datetime
declare @seconds int
set @start = getdate()
set rowcount @rowcount
exec sp_executesql @sql
while @@rowcount >= @rowcount
begin
set @seconds = datediff(second, @start, getdate())
set @message = convert(nvarchar(max), @seconds) + ':' + convert(nvarchar(max), @rowcount)
raiserror(@message, 0, 1) with nowait
if @seconds < @maxseconds
set @rowcount = @rowcount * 15 / 10
else
set @rowcount = @rowcount * 10 / 15
set rowcount @rowcount
waitfor delay @waitseconds
set @start = getdate()
exec sp_executesql @sql
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment