Change Database Recover Model

Change Database Recover Model

USE master
DECLARE @isql VARCHAR(2000),
        @dbname VARCHAR(64),
        @logfile VARCHAR(128)

DECLARE c1 CURSOR FOR
SELECT d.name,
       mf.name AS logfile --, physical_name AS current_file_location, size
FROM sys.master_files mf
    INNER JOIN sys.databases d
        ON mf.database_id = d.database_id
WHERE recovery_model_desc <> 'SIMPLE'
      AND d.name NOT IN ( 'master', 'model', 'msdb', 'tempdb' )
      AND mf.type_desc = 'LOG'
OPEN c1
FETCH NEXT FROM c1
INTO @dbname,
     @logfile
WHILE @@fetch_status <> -1
BEGIN
    SELECT @isql = 'ALTER DATABASE ' + @dbname + ' SET RECOVERY SIMPLE'
    PRINT @isql
    --exec(@isql)
    SELECT @isql = 'USE ' + @dbname + ' checkpoint'
    PRINT @isql
    --exec(@isql)
    SELECT @isql = 'USE ' + @dbname + ' DBCC SHRINKFILE (' + @logfile + ', 1)'
    PRINT @isql
    --exec(@isql)

    FETCH NEXT FROM c1
    INTO @dbname,
         @logfile
END
CLOSE c1
DEALLOCATE c1

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *