TSQL – Set all databases SIMPLE recovery

Use the below TSQL script to set all databases to simple recovery mode.   Helpful for development environments and lab VMs to save on disk storage and backup space.

Cheers!  shades_smile

Code

USE MASTER
declare
@isql varchar(2000),
@dbname varchar(64)
declare c1 cursor for select name from master..sysdatabases where name not in ('master','model','msdb','tempdb','ReportServer','ReportServerTempDB')
open c1
fetch next from c1 into @dbname
While @@fetch_status <> -1
    begin
	
    select @isql = 'ALTER DATABASE @dbname SET RECOVERY SIMPLE'
    select @isql = replace(@isql,'@dbname',@dbname)
    print @isql
    exec(@isql)
    select @isql='USE @dbname; DBCC SHRINKFILE (N''@dbname_log'' , 0, TRUNCATEONLY)'
    select @isql = replace(@isql,'@dbname',@dbname)
    print @isql
    exec(@isql)
    fetch next from c1 into @dbname
    end
close c1
deallocate c1
Return to Top ▲Return to Top ▲