Skip to content

Commit cc3804c

Browse files
committed
Add new parameter @RegrowIntervalMB
1 parent d61585d commit cc3804c

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

Utility Scripts/Shrink_Database_File_in_Specified_Increments.sql

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Description:
1919
----------------------------------------------------------------------------
2020
2121
Change log:
22+
2026-06-29 - Eitan - Added new parameter: @RegrowIntervalMB
2223
2026-06-24 - Eitan - Added new parameter: @RegrowOnRetry
2324
2025-06-18 - Eitan - Added new parameters: @MaxActiveTranLogSizeMB, @MaxActiveTranLogSizePct, and @ActiveTranLogSeverity
2425
2024-09-29 - Vitaly - Added a time limit (maximum execution duration) for the process (@TimeLimit and @RunStartTime parameters)
@@ -49,6 +50,7 @@ DECLARE
4950
,@IterationMaxRetries INT = 3 -- Maximum number of attempts per iteration to shrink a file, when cannot successfuly shrink to desired target size
5051
,@RegrowOnError5240 BIT = 1 -- Error 5240 may be resolved by temporarily increasing the file size before shrinking it again.
5152
,@RegrowOnRetry BIT = 1 -- Inability to shrink may be resolved by temporarily increasing the file size before shrinking it again.
53+
,@RegrowIntervalMB INT = NULL -- Determine by which interval to re-grow the file upon retry. Set to NULL to use same value as @IntervalMB.
5254

5355
,@AGReplicaLinkedServer SYSNAME = NULL -- Linked Server name of the AG replica to check. Leave as NULL to ignore.
5456
,@MaxReplicaRecoveryQueue INT = 10000 -- Maximum recovery queue of AG replica (in KB). Use this to prevent overload on the AG.
@@ -97,6 +99,14 @@ BEGIN
9799
GOTO Quit;
98100
END
99101

102+
IF @RegrowIntervalMB < 1
103+
BEGIN
104+
RAISERROR(N'@RegrowIntervalMB must be an integer value of 1 or higher (or NULL if you want to use same value as @IntervalMB)', 16,1)
105+
GOTO Quit;
106+
END
107+
108+
SET @RegrowIntervalMB = ISNULL(@RegrowIntervalMB, @IntervalMB);
109+
100110
SET @sp_executesql = QUOTENAME(@DatabaseName) + '..sp_executesql'
101111

102112
DECLARE @RecoveryQueueCheckCmd NVARCHAR(4000), @RecoveryQueueCheckParams NVARCHAR(4000), @PartnerServer SYSNAME, @RecoveryQueue INT
@@ -280,7 +290,7 @@ BEGIN
280290
IF @RegrowOnError5240 = 1 AND ERROR_NUMBER() = 5240
281291
BEGIN
282292
-- This error can be solved by increasing the file size a bit before shrinking again
283-
SET @CMD = N'ALTER DATABASE ' + QUOTENAME(@DatabaseName) + N' MODIFY FILE (NAME = ' + QUOTENAME(@FileName, N'''') + N', SIZE = ' + CONVERT(nvarchar(1000), @CurrSizeMB + @IntervalMB) + N'MB); -- ' + CONVERT(nvarchar(25),GETDATE(),121)
293+
SET @CMD = N'ALTER DATABASE ' + QUOTENAME(@DatabaseName) + N' MODIFY FILE (NAME = ' + QUOTENAME(@FileName, N'''') + N', SIZE = ' + CONVERT(nvarchar(1000), @CurrSizeMB + @RegrowIntervalMB) + N'MB); -- ' + CONVERT(nvarchar(25),GETDATE(),121)
284294

285295
PRINT N'-- Error 5240 encountered. Regrowing:'
286296
RAISERROR(N'%s',0,1,@CMD) WITH NOWAIT;
@@ -312,7 +322,7 @@ BEGIN
312322
IF @RegrowOnRetry = 1
313323
BEGIN
314324
-- This error can be solved by increasing the file size a bit before shrinking again
315-
SET @CMD = N'ALTER DATABASE ' + QUOTENAME(@DatabaseName) + N' MODIFY FILE (NAME = ' + QUOTENAME(@FileName, N'''') + N', SIZE = ' + CONVERT(nvarchar(1000), @CurrSizeMB + @IntervalMB) + N'MB); -- ' + CONVERT(nvarchar(25),GETDATE(),121)
325+
SET @CMD = N'ALTER DATABASE ' + QUOTENAME(@DatabaseName) + N' MODIFY FILE (NAME = ' + QUOTENAME(@FileName, N'''') + N', SIZE = ' + CONVERT(nvarchar(1000), @CurrSizeMB + @RegrowIntervalMB) + N'MB); -- ' + CONVERT(nvarchar(25),GETDATE(),121)
316326

317327
PRINT N'-- Regrowing:'
318328
RAISERROR(N'%s',0,1,@CMD) WITH NOWAIT;

0 commit comments

Comments
 (0)