Truncate The Transaction Log of Every Database on a Microsoft SQL Server

Posted by sam Fri, 12 Feb 2010 16:05:00 GMT

Somewhat hackish and lacking alerting/error handling, but good enough to keep an internal or development database machine somewhat tidy:

declare@cmd1 varchar(500)
declare@cmd2 varchar(500)
declare@cmd3 varchar(500)
declare@cmd4 varchar(500)

set@cmd1 =
'IF ("?" NOT IN ("master", "tempdb", "msdb", "model")) print "*** Processing DB ? ***"'

set@cmd2 ='IF ("?" NOT IN ("master", "tempdb", "msdb", "model")) execute("alter database ? set recovery simple with no_wait")'
set@cmd3 ='IF ("?" NOT IN ("master", "tempdb", "msdb", "model")) execute("dbcc shrinkdatabase (?)")'

-- Execute the first 3 commands
execsp_MSforeachdb @command1=@cmd1,@command2=@cmd2,@command3=@cmd3

-- As we can only pass 3 commands to this sp_, iterate around again to restore the recovery mode.
set@cmd4 ='IF ("?" NOT IN ("master", "tempdb", "msdb", "model")) execute("alter database ? set recovery full with no_wait")'
execsp_MSforeachdb @command1=@cmd4  
Trackbacks

Use the following link to trackback from your own site:
http://sam-pointer.com/trackbacks?article_id=39