Solved

Script to change ALL databases recovery mode to Full

Posted on 2010-08-31
9
1,242 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 500 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
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.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

761 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