Skip to content

Instantly share code, notes, and snippets.

@peschkaj
Created September 14, 2012 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peschkaj/3724770 to your computer and use it in GitHub Desktop.
Save peschkaj/3724770 to your computer and use it in GitHub Desktop.
DECLARE @rVal AS BIT ;
DECLARE @dbName AS sysname ;
DECLARE @command AS NVARCHAR(MAX)
DECLARE @error AS NVARCHAR(MAX) ;
DECLARE @newline AS NVARCHAR(2) ;
SET @error = N'' ;
SET @newline = NCHAR(13) + NCHAR(10) ;
DECLARE shrinklog CURSOR FAST_FORWARD READ_ONLY
FOR SELECT N'DBCC SHRINKFILE (' + DatabaseName + N'_Log, '
+ CAST(LogFileSizeInMB AS NVARCHAR(10)) + N') ;',
DatabaseName
FROM Maintenance.dbo.BackupControl AS bc
WHERE LogFileSizeInMB IS NOT NULL
OPEN shrinklog
FETCH NEXT FROM shrinklog INTO @command, @dbName
WHILE @@FETCH_STATUS = 0
BEGIN
EXECUTE @rval = sp_executesql @command ;
IF @rVal <> 0
SET @error = N'Error truncating log file for database ' + @dbName;
FETCH NEXT FROM shrinklog INTO @command, @dbName
END
CLOSE shrinklog
DEALLOCATE shrinklog
IF @error <> ''
RAISERROR(50000, 16, 1, @error) WITH NOWAIT, LOG, SETERROR ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment