Truncate The Transaction Log of Every Database on a Microsoft SQL Server
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

