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
--找出所有的Log文件,Log文件的status是0x40 select fileid from sysfiles where status & 0x40 = 0x40 open LogFiles fetch next from LogFiles into @iLogFile while @@fetch_status = 0 begin
--使用DBCC名字縮短Log文件 set @tsql = @tsql + ''DBCC SHRINKFILE(''+cast(@iLogFile as varchar(5))+'', 1) '' fetch next from LogFiles into @iLogFile end
--DBCC shrink只能釋放標記為無效的Log區段,使用backup log可以完成這個標記 set @tsql = @tsql + '' BACKUP LOG [?] WITH TRUNCATE_ONLY '' + @tsql --PRint @tsql exec(@tsql) close LogFiles DEALLOCATE LogFiles end' --依次遍歷所有的數據庫,用數據庫名字替換@ssql中的?,并執行語句 exec sp_msforeachdb @ssql