DECLARE @name VARCHAR(25)
DECLARE @SQL VARCHAR(1000)
DECLARE @logid INT
DECLARE sysdatabase_name CURSOR FOR SELECT name FROM master.dbo.sysdatabases
OPEN sysdatabase_name
FETCH NEXT FROM sysdatabase_name INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN
IF ( @name NOT IN ('xxx')) --不需要进行日志收缩的数据库名
BEGIN
SET @SQL =' DECLARE @logid INT
USE ' + @name+'
SELECT @logid = fileid FROM sysfiles WHERE RIGHT(rtrim(filename),3) = ''ldf''
BACKUP LOG ' + @name+' WITH NO_LOG
DBCC SHRINKFILE (@logid) '
EXEC(@SQL)
END
FETCH NEXT FROM sysdatabase_name INTO @name
END
CLOSE sysdatabase_name
DEALLOCATE sysdatabase_name
--转自