Solved

Script to change ALL databases recovery mode to Full

Posted on 2010-08-31
9
1,203 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
  • 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Truncate vs Delete 63 90
Bubble user-defined Sql RAISERROR(...) to c# exception 14 118
using BEGIN END with IF statement.. 12 40
Sql query 34 22
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now