?
Solved

Script to change ALL databases recovery mode to Full

Posted on 2010-08-31
9
Medium Priority
?
1,263 Views
Last Modified: 2012-08-13
I am after a script to run that will change the recovery model of all databases to FULL and not just one at a time.

Can anyone please help?
0
Comment
Question by:Avatar261
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 41

Expert Comment

by:ralmada
ID: 33568218
try
EXEC sp_MSForEachDB 'ALTER DATABASE [?] SET RECOVERY FULL';

Open in new window

0
 
LVL 2

Author Comment

by:Avatar261
ID: 33568539
Brilliant, just as a side thought does it matter if master, model are in FULL mode?
0
 
LVL 41

Expert Comment

by:ralmada
ID: 33568622
That doesn't matter to run the above script.
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 6

Expert Comment

by:anushahanna
ID: 33569661
But you won't be able to do a FULL mode on tempdb

you will get "Option 'RECOVERY' cannot be set in database 'tempdb'."
0
 
LVL 6

Expert Comment

by:anushahanna
ID: 33569675
you can try something like

EXEC sp_MSforeachdb '
IF ''?'' IN (''master'', ''model'', ''msdb'', ''tempdb'')
    RETURN
ALTER DATABASE [?] SET RECOVERY FULL
'

to avoid hitting tempdb in your script, (and any other system db's you want to avoid)

I have not tested the above code, yet.
0
 
LVL 41

Accepted Solution

by:
ralmada earned 2000 total points
ID: 33571804
if you want to avoid the system dbs then do like below:

EXEC sp_MSforeachdb 'IF [?] NOT IN (''master'', ''model'', ''msdb'', ''tempdb'')
		ALTER DATABASE [?] SET RECOVERY FULL'

Open in new window

0
 
LVL 6

Expert Comment

by:anushahanna
ID: 33579627
ralmada, you have guided me often with sp_MSforeachdb. Thanks again..

now, with a student's cap:

EXEC sp_MSforeachdb 'IF [?] NOT IN (''master'', ''model'', ''msdb'', ''tempdb'')
            ALTER DATABASE [?] SET RECOVERY FULL'

gives multiple errors..

if I change it to
EXEC sp_MSforeachdb 'IF "?" NOT IN (''master'', ''model'', ''msdb'', ''tempdb'')
            ALTER DATABASE [?] SET RECOVERY FULL'
it does not give the previous errors, but still gives the error:

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

that does not make sense.. even though we told to avoid tempdb, why does it try it again on tempdb?

also, I tried playing with db_name(), but no go..

muchas gracias :)
0
 
LVL 2

Author Comment

by:Avatar261
ID: 33596100
After checking a bit more after accepting the solution i to was getting the tempdb error.

After tweaking the script slightly, this works prefectly. The solution still stands as it was on the right lines.

EXEC sp_MSforeachdb 'IF ''?'' NOT IN (''master'', ''model'', ''msdb'', ''tempdb'')  
Begin  
Print ''?''
Declare @cmd varchar(255)
set @cmd = ''ALTER DATABASE [?] SET RECOVERY FULL''
exec (@cmd)
End'
0
 

Expert Comment

by:sandsjh
ID: 37550089
EXEC sp_MSforeachdb 'IF [?] NOT IN (''master'', ''model'', ''msdb'', ''tempdb'')
		ALTER DATABASE [?] SET RECOVERY FULL'

Open in new window


Hi all: I used the above solution but keep getting the following:
Msg 207, Level 16, State 1, Line 1
Invalid column name 'sandsjh1_123_db'.

Open in new window


Hoe to avoid this on 500 db's?

TIA.
Jason
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question