Use a SELECT to ALTER properties of all databases of certain type...

  • So I was flipping the DBs on one of my QA servers from FULL to SIMPLE recovery (we don't need PiT recovery in QA) and got thinking...

    Yes, that explains the fire trucks you heard go by a few minutes ago smart guy...

    :hehe:

    Anyways.

    Is it possible to write and ALTER statement something like this:

    ALTER DATABASE (SELECT NAME FROM sys.databases

    WHERE recovery_model_desc = N'SIMPLE'

    AND name NOT IN ('master','model','msdb','tempdb'))

    SET RECOVERY SIMPLE WITH NO_WAIT;

    Yes, I know this could be a potentially dangerous trick (just replace 'set recovery' with 'set offline' for some *real* fun) but when you've got a bunch of DBs to switch, it's quicker than copy/paste, and easier to key up then using sp_msforeachdb (which as someone pointed out on SQLSkills I believe, doesn't always work right)

    Regardless, I've already done the copy-paste method, but for future reference...

    Thanks,

    Jason

  • As far as I know you can only do something like through ad hoc mechanisms or programming (PowerShell to the rescue).

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • Keeping in mind that you'll be running it from a script yourself and not exposing it to the internet, how about something like this?

    SELECT 'ALTER DATABASE ' + name + ' SET RECOVERY SIMPLE WITH NO_WAIT;'

    FROM sys.databases

    WHERE recovery_model_desc = 'SIMPLE'

    AND name NOT IN ('master','model','msdb','tempdb')

    ORDER BY name;

  • Put the dynamic sql into a variable and Execute the variable.

    DECLARE @SQL varchar(max) = '';

    SELECT @SQL = @SQL + '

    ALTER DATABASE ' + name + ' SET RECOVERY SIMPLE WITH NO_WAIT;'

    FROM sys.databases

    WHERE recovery_model_desc = 'SIMPLE'

    AND name NOT IN ('master','model','msdb','tempdb')

    ORDER BY name;

    Execute (@SQL);

    Also, your query shows you are setting your database to simple recovery if they are already in simple recovery mode. Is that what you want? Should that be where NOT recovery_model_desc = 'SIMPLE'

    Wes
    (A solid design is always preferable to a creative workaround)

  • D'OH!

    The WHERE recovery_model_desc = 'SIMPLE' should've been = 'FULL'

    Thanks.

    I hadn't thought of the dynamic SQL idea, although I'm now messing with that to try to check what DBs SQL Logins might have access to before dropping them...

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply