Shrink multiple databases at a time

DECLARE @logName as nvarchar(50)

DECLARE @databaseID as int

DECLARE db_cursor CURSOR FOR

SELECT TOP 10 name,database_id -- only 10 but you can choose any number

FROM sys.master_Files WHERE physical_name like '%.ldf'

and physical_name not like 'C:\%'  -- specify your database paths

and name not in ('mastlog') -- any database logs that you would like to exclude

ORDER BY size DESC

OPEN db_cursor

FETCH NEXT FROM db_cursor INTO @logName , @databaseID

WHILE @@FETCH_STATUS = 0

BEGIN

    DECLARE @databaseName as nvarchar(50)

 SET @databaseName =  DB_NAME(@databaseID)

  DECLARE @tsql nvarchar(300)

 SET @tsql='USE ['+@databaseName+'] ALTER DATABASE ['+@databaseName+'] set recovery simple DBCC SHRINKFILE ('+@logName+' , 1)'

 EXEC(@tsql)

    FETCH NEXT FROM db_cursor INTO @logName , @databaseID

END

CLOSE db_cursor

DEALLOCATE db_cursor

Comments

Post a Comment

Hi User,
Thanks for visiting My Blog and please provide your valuable feedback and subscribe for more updates. Please don't post any spam content or comments.
Thank You

Popular Posts

Failed to execute the package or element. Build errors were encountered

Restore of database 'DataBase_Name' failed. (Microsoft.SqlServer.Management.RelationalEngineTasks)

Cannot convert "Column" between a unicode and a non-unicode string data types in SSIS