Truncate SharePoint Log Files for All the database in SQL Server

April 23, 2014 — Leave a comment

Truncate Log Files for All the database in SQL Server

1. Perform full backup of all your database
2. Open your SQL Management Studio

3. Click on New Query
4. Insert the code and click Execute

clip_image002


SQL 2008

use master
DECLARE @Statement varchar (4000)
SELECT @Statement = ”
SELECT @Statement = @Statement + ‘USE [?];’
SELECT @Statement = @Statement + ‘DECLARE @Log_Logical_FileName varchar (100);’
SELECT @Statement = @Statement + ‘SELECT @Log_Logical_FileName = rtrim(name) FROM dbo.sysfiles WHERE fileid = 2; ‘
SELECT @Statement = @Statement + ‘ALTER DATABASE [?] SET RECOVERY SIMPLE WITH NO_WAIT;’
SELECT @Statement = @Statement + ‘DBCC SHRINKFILE(@Log_Logical_FileName,1);’
SELECT @Statement = @Statement + ‘ALTER DATABASE [?] SET RECOVERY FULL WITH NO_WAIT;’
EXEC sp_MSforeachdb @command1=@Statement
GO

SQL 2005

use master
DECLARE @Statement varchar (4000)
SELECT @Statement = ”
SELECT @Statement = @Statement + ‘USE [?]; ‘
SELECT @Statement = @Statement + ‘DECLARE @Log_Logical_FileName varchar (100); ‘
SELECT @Statement = @Statement + ‘SELECT @Log_Logical_FileName = rtrim(name) FROM dbo.sysfiles WHERE fileid = 2; ‘
SELECT @Statement = @Statement + ‘BACKUP LOG [?] WITH TRUNCATE_ONLY; ‘
SELECT @Statement = @Statement + ‘DBCC SHRINKFILE(@Log_Logical_FileName,1); ‘
EXEC sp_MSforeachdb @command1=@Statement
GO

Running this will truncate all your log file

No Comments

Be the first to start the conversation!

Leave a Reply

Please log in using one of these methods to post your comment:

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