Welcome to Julian Kuiters Monday, May 21 2018 @ 07:42 AM AEST

Shrinking Databases bit-by-bit to avoid long delays

  • Contributed by:
SQL Server 2005 Michael Jones has a neat script for shrinking databases in small chunks to avoid having the server locked up for a long period.

declare @sql varchar(8000)
declare @name sysname
declare @sizeMB int
declare @UsedMB int
declare @FreeMB int
declare @ShrinkMB int
-- Desired free space in MB after shrink set @FreeMB = 1000
-- Increment to shrink file by in MB set @ShrinkMB = 50
-- Name of Database file to shrink set @name = 'MyDatabaseFileName'
-- Get current file size in MB select @sizeMB = size/128. from sysfiles where name = @name
-- Get current space used in MB select @UsedMB = fileproperty( @name,'SpaceUsed')/128.
select [StartFileSize] = @sizeMB, [StartUsedSpace] = @UsedMB, [File] = @name
-- Loop until file at desired size while  @sizeMB > @UsedMB+@FreeMB+@ShrinkMB   begin
  set @sql =   'dbcc shrinkfile ( ' + @name + ', '+convert(varchar(20),@sizeMB-@ShrinkMB)+' ) '
  print 'Start ' + @sql
  exec ( @sql )
  print 'Done ' + @sql
  -- Get current file size in MB   select @sizeMB = size/128. from sysfiles where name = @name     -- Get current space used in MB   select @UsedMB = fileproperty( @name,'SpaceUsed')/128.
  end
select [EndFileSize] = @sizeMB, [EndUsedSpace] = @UsedMB, [File] = @name