Archive for October, 2016

shrink db file script in increments

October 5, 2016
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