Welcome to EMC Consulting Blogs Sign in | Join | Help

Claypole's World - The SQL Server Side

Concerning development of bespoke applications and database administration.

SQL Server Database Recovery Model: How do you set yours?

We had the classic situation come up on a project the other day.  The recovery models on the databases were set to full and yet the backup strategy didn't reflect this.  Needless to say, we ran out of disk space on our build server.  In this case the answer was to change the recovery models over to simple.  However, I wanted to do this quickly and painlessly. This is how I did it.

First of all I confirmed that this was indeed the case by checking all the recovery models on the server.

Select   [name]
        ,state_desc 
        ,recovery_model_desc 
From    sys.databases

Before (Stunning graphical editing powers to protect the innocent I know. Perhaps I should be having a word with Matt and our amazing creative team... )

This told me that as I suspected every database save tempdb was in the full recovery model.  N.B. tempdb as we all know is "special".  It can only be in the simple recovery model and if you try to change it then you will get the following error.

Msg 5058, Level 16, State 1, Line 1
Option 'RECOVERY' cannot be set in database 'tempdb'.

So how did I change this.  I needed to accomplish two things.

  1. Change a lot of databases to the simple recovery model
  2. Leave tempdb alone to avoid raising the error message

I opted to use my old friend sp_MSforeachdb. sp_MSforeachdb is not documented in Books Online so I guess it's not officially supported. However, it is extremely useful at times like this.  Sp_MSforeachdb opens up an OLEDB connection to the sql server and loops through the DB's so that you can execute a statement against each one. Yes that does mean a cursor - but at least I haven't had to write all that logic.  However, because it does it this way you can also parameterise your script using the ?  as a parameter.  If this proc has escaped your attention up till now I am sure you will also be delighted to know that it has a little sister called sp_MSforeachtable.  I'll let you work out what that does.  However, there is a pretty good write up here on Database Journal.

So anyways back to the story. I was left with a script that looked like this:

exec sp_msforeachdb 'IF ''?'' <> ''tempdb'' Begin EXEC('' alter database [?] set recovery simple '') print ''?'' + '' recovery model altered '' End'

For the keenly observant you will notice that I have put my alter database command inside an exec statement.  This was due to the fact that SQL Server continued  to raise the error mentioned above even though I had specifically coded around the problem.  I will go into more detail on why this happens in another post.

A quick look at sys.databases confirms that all the recovery models are now set to simple. Now all that needs to be done is for the log files to be re-sized...

After (hey where did the blue highlighting go... oh that's not very consistent...)

Cheers, James

Published 15 July 2008 10:07 by James.Rowland-Jones

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Alex said:

Advise use-<a href="http://www.recoverytoolbox.com/sql.html">sql database recovery</a>,

repair data from corrupted databases in the MS SQL Server format (files with the *.mdf extension),can search for and single out housekeeping data that can be used to repair some data modules,save recovered data as SQL scripts, it is also possible to split data into files of any size,can save extracted information directly to the SQL server. It allow to recovery SQL Server faster,compatible with all supported versions of Microsoft Windows, such as Windows 98, Windows Me, Windows NT 4.0, Windows 2000, Windows XP, Windows XP SP2, Windows 2003 Server, Windows Vista.

October 15, 2008 22:10
 

Alex said:

This link is more right-[url=http://www.recoverytoolbox.com/sql.html]sql database recovery[/url]

October 15, 2008 22:11
 

jwelch said:

Brilliant! - just what I was looking for.

December 12, 2008 19:12
 

repair sql server database said:

Know good tool which recover data also it restore sql files too-repair sql server database,it is free as how as i remember,utility repair data from corrupted databases in the MS SQL Server format (files with the *.mdf extension),supports data extraction via the local area network,can save recovered data as SQL scripts, it is also possible to split data into files of any size,compatible with all supported versions of Microsoft Windows, such as Windows 98, Windows Me, Windows NT 4.0, Windows 2000, Windows XP, Windows XP SP2, Windows 2003 Server, Windows Vista,tool supports the following database formats: Microsoft SQL Server 7.0, 2000, 2005,also can repair .mdf files of Microsoft SQL Server 2005, repair mdf file of Microsoft SQL Server 2005 (64-bit).

March 26, 2009 15:47

Leave a Comment

(required) 
(optional)
(required) 
Submit

About James.Rowland-Jones

James is an Advisory Practice Consultant with EMC Consulting. He works primarily with SQL Server technologies in architecture, development and administration capacities. He also co-ordinates our Internal SQL Server Community.

View James Rowland-Jones's profile on LinkedIn

Powered by Community Server (Personal Edition), by Telligent Systems