Technical Article

Shrink Log file for all Databases

,

I use this script to shrink the log files for development databases. The intention is keep the log files as small as possible when the database is not in use. By scheduling this script I can control when it happens as oppose to using auto shrink.

--  This script shrinks the log file for all the databases in the instance
use master 
DECLARE @Statement varchar (2000)

SELECT @Statement = ''
SELECT @Statement = @Statement + 'USE ?; '
SELECT @Statement = @Statement + 'SELECT ''?''; '
SELECT @Statement = @Statement + 'DECLARE @Log_Logical_FileName varchar (30); '
SELECT @Statement = @Statement + 'SELECT @Log_Logical_FileName = rtrim(name) FROM dbo.sysfiles WHERE (status & 0x40) <> 0; '
SELECT @Statement = @Statement + 'dbcc shrinkfile (@Log_Logical_FileName, 30,truncateonly); '
SELECT @Statement = @Statement + 'SELECT fileid, name, filename, size, growth, status, maxsize FROM dbo.sysfiles WHERE (status & 0x40) <> 0; '
SELECT @Statement 

EXEC sp_MSforeachdb @command1=@Statement
GO

Rate

1.5 (2)

You rated this post out of 5. Change rating

Share

Share

Rate

1.5 (2)

You rated this post out of 5. Change rating