--Variables for cursors
DECLARE @DBName nvarchar(100)
--Cursor for temp..LogFiles filling
DECLARE Cursor_for_EnumDBs CURSOR
FOR SELECT name FROM sys.databases
WHERE name <> 'master' AND
name <> 'tempdb' AND
name <> 'model' AND
name <> 'msdb' AND
name <> 'NoShrinkLogDB'
ORDER BY name;
OPEN Cursor_for_EnumDBs
FETCH NEXT FROM Cursor_for_EnumDBs INTO @DBName WHILE @@FETCH_STATUS = 0
BEGIN
--Dynamic query for Transaction Log shrink
EXEC('
USE ' + @DBName + ';
DECLARE @LogName nvarchar(100);
SELECT name INTO #LogFiles FROM sys.database_files WHERE type = 1 AND state = 0;
DECLARE Cursor_for_EnumLogFiles CURSOR FOR SELECT * FROM #LogFiles
OPEN Cursor_for_EnumLogFiles
FETCH NEXT FROM Cursor_for_EnumLogFiles INTO @LogName WHILE @@FETCH_STATUS = 0
BEGIN
DBCC SHRINKFILE(@LogName);
FETCH NEXT FROM Cursor_for_EnumLogFiles INTO @LogName
END
CLOSE Cursor_for_EnumLogFiles
DEALLOCATE Cursor_for_EnumLogFiles
')
FETCH NEXT FROM Cursor_for_EnumDBs INTO @DBName
END
CLOSE Cursor_for_EnumDBs
DEALLOCATE Cursor_for_EnumDBs