Link to home
Start Free TrialLog in
Avatar of Avatar261
Avatar261

asked on

Script to change ALL databases recovery mode to Full

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?
Avatar of ralmada
ralmada
Flag of Canada image

try
EXEC sp_MSForEachDB 'ALTER DATABASE [?] SET RECOVERY FULL';

Open in new window

Avatar of Avatar261
Avatar261

ASKER

Brilliant, just as a side thought does it matter if master, model are in FULL mode?
That doesn't matter to run the above script.
But you won't be able to do a FULL mode on tempdb

you will get "Option 'RECOVERY' cannot be set in database 'tempdb'."
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.
ASKER CERTIFIED SOLUTION
Avatar of ralmada
ralmada
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 :)
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'
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