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

SQL Server (T-SQL)

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

Setting the Recovery Model of a database to Simple

We run a web hosting company and we use SQL Server, it happens that we put up several new SQL databases a month. Invariably, the developer will forget to put the new database in 'Simple' recovery model. If you leave a database in 'Full' mode and only run regular backups, the transaction logs won't truncate and the drive will fill up.

 That's what happened today (sigh), so I searched and found a script on http://richardteachout.com/Blog/tabid/165/EntryID/64/Default.aspx. I have made a small mod and I am reprinting the script here so that I don't lose it.

 USE master
GO


-- Declare a variable to store the value [database name] returned by FETCH.
DECLARE @dbname sysname, @cmd varchar(1000)


-- Declare a cursor to iterate through the list of databases
DECLARE db_recovery_cursor CURSOR FOR 
SELECT name from sysdatabases
-- Open the cursor
OPEN db_recovery_cursor


-- Perform the first fetch and store the value in a variable.
FETCH NEXT FROM db_recovery_cursor INTO @dbname


-- loop through cursor until no more records fetched
WHILE @@FETCH_STATUS = 0


BEGIN
IF (SELECT DATABASEPROPERTYEX(@dbname,'RECOVERY')) <> 'SIMPLE' and @dbName <> 'tempdb' BEGIN


-- create the alter database command for each database
SET @cmd = 'ALTER DATABASE ' + @dbname + ' SET RECOVERY SIMPLE'


-- alter each dataabase setting the recovery model to FULL
EXEC(@cmd)


PRINT @dbname 
end


FETCH NEXT FROM db_recovery_cursor INTO @dbname
END


-- close the cursor and deallocate memory used by cursor
CLOSE db_recovery_cursor


DEALLOCATE db_recovery_cursor


 

As always, your comments are welcome. If this post was helpful, please link to our site.

 

Comments

 

Ryan Davis said:

Thank you so much for posting this, I've been slowly auditing a bunch of servers and this code snippet is very handy.

One problem is that this leaves existing log files hanging around taking up space on disk.  To clear those, you can issue a couple more commands after setting to simple recovery:

BACKUP LOG database_name WITH TRUNCATE_ONLY

DBCC SHRINKDATABASE(database_name)

There are probably more efficient ways to do that, but that version works for me.

June 29, 2007 12:23 PM [Delete]
 

Pete said:

This script was a huge help to me. The only thing I would change in this script is adding a double quote for the databases that have a hyphens in them. Certain applications add these hyphens in the database name. I am not certain why they do this, but the script will fail on those databases if the double quotes are not used.

SET @cmd = 'ALTER DATABASE "' + @dbname + '" SET RECOVERY SIMPLE'

February 26, 2008 7:13 PM [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.