Cool Script to Truncate log file from all databases. Very cool

Posted by Rajesh Setty on Wednesday, May 12, 2010
In this post I am posting link, for truncating all log files in Database. link: http://sharepoint-geek.com/2010/04/07/shrink-all-log-files-within-a-ms-sql-instance/

Snapshot of the Script Just incase if removed from above website:
declare @ssql nvarchar(4000)
set @ssql= '
        if ''?'' not in (''tempdb'',''master'',''model'',''msdb'') begin
        use [?]
        declare @tsql nvarchar(4000) set @tsql = ''''
        declare @iLogFile int
        declare LogFiles cursor for
        select fileid from sysfiles where  status & 0x40 = 0x40
        open LogFiles
        fetch next from LogFiles into @iLogFile
        while @@fetch_status = 0
        begin
          set @tsql = @tsql + ''DBCC SHRINKFILE(''+cast(@iLogFile as varchar(5))+'', 1) ''
          fetch next from LogFiles into @iLogFile
        end
        set @tsql = @tsql + '' BACKUP LOG [?] WITH TRUNCATE_ONLY '' + @tsql
        --print @tsql
        exec(@tsql)
        close LogFiles
        DEALLOCATE LogFiles
        end'
exec sp_msforeachdb @ssql


Tags: t-sql truncate log file in database 
Make a Free Website with Yola.