Site Search:
Sign in | Join | Help
4Penny.net

SQL Server (T-SQL)

Comments and notes on SQL Server 2000, 2005, and T-SQL

Change the Recovery Model of All Databases on a SQL Server

Today's tip is a nice piece of code that will allow you to change the  recovery model of all databases on a server.

You take all the time to set up a database backup plan for a server, and someone sticks a new database on it that does not conform to your backup schema. Depending on whether or not you backup the transaction logs, you'll need the Recovery Model to be set to 'simple' or 'full'. Tyically if one database is set the wrong way, your finely tuned backup plan will fail.

--delcare variables
declare @vchrTable varchar(200),
 @vchrMsg varchar(200)

--declare the cursor
DECLARE curTables CURSOR FOR
 SELECT name from master..sysdatabases where not name in ('tempdb')

--open the cursor
OPEN curTables

--loop through the cursor lines
FETCH NEXT FROM curTables INTO @vchrTable
WHILE @@FETCH_STATUS = 0
BEGIN
 set @vchrMsg = 'ALTER DATABASE [' + @vchrTable + '] SET RECOVERY SIMPLE;'
 print @vchrMsg
 exec (@vchrMsg)

 FETCH NEXT FROM curTables INTO @vchrTable
END

--clean up
CLOSE curTables
DEALLOCATE curTables

Comments

 

rmariboe said:

Works splendidly ;)

January 18, 2008 4:23 AM [Delete]

Leave a Comment

(required)  
(optional)
(required)  
Add

About Steve Gray

Steve is a seasoned (translate: old) developer in VB and ASP.NET. He spends most of his time in Dynamics GP, writing custom mods for consulting firms. Crystal reports, eConnect, VS Tools for Dynamics... anything that comes along.