2019-06-10 21:15:46 -07:00
|
|
|
-- Enter the file name you want to shrink and target file size
|
2019-12-26 17:27:48 -08:00
|
|
|
-- File will be shrinked in a loop by one gigabyte at a time
|
|
|
|
|
DECLARE @FileName VARCHAR(100) = 'file_name';
|
2019-06-10 21:15:46 -07:00
|
|
|
DECLARE @DesiredSize INT = 30000;
|
|
|
|
|
|
|
|
|
|
DECLARE @CurrentSize INT;
|
2019-06-11 13:43:33 -07:00
|
|
|
SELECT @CurrentSize = ROUND(size * 8 / 1024, -3) FROM sys.database_files WHERE name = @FileName
|
2019-06-10 21:15:46 -07:00
|
|
|
|
|
|
|
|
WHILE (@CurrentSize > @DesiredSize)
|
|
|
|
|
BEGIN
|
|
|
|
|
|
|
|
|
|
SET LOCK_TIMEOUT 5000; -- this supposed to help against heavy blocking (but I don't think it works)
|
|
|
|
|
|
|
|
|
|
DBCC SHRINKFILE (@FileName , @CurrentSize);
|
|
|
|
|
|
|
|
|
|
PRINT @FileName + ' new size is: ' + FORMAT(@CurrentSize, 'N0');
|
|
|
|
|
|
|
|
|
|
SET @CurrentSize = @CurrentSize - 1000;
|
|
|
|
|
|
|
|
|
|
END
|