Last active
July 28, 2022 07:09
-
-
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
This file contains 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
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