shrink db file script in increments

DECLARE @startvalue BIGINT = 250000
,@targetsize BIGINT = 50000
,@stepsize BIGINT = 1000
,@filename VARCHAR(MAX) = 'xxx'

WHILE @startvalue >=@targetsize
BEGIN

SET @startvalue = @startvalue-@stepsize

DECLARE @shrinkstatement VARCHAR(MAX) = 
 'DBCC SHRINKFILE(N''' + @filename + ''',' + CAST(@startvalue AS VARCHAR(100)) + ')'

PRINT @shrinkstatement
 EXEC (@shrinkstatement)

END
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: