sp_MSForeachdb to exclude master DB. doesnt seem to work

  • EXEC master..sp_MSForeachdb '

    USE [?]

    IF ''?'' <> ''master'' AND ''?'' <> ''model'' AND ''?'' <> ''msdb'' AND ''?'' <> ''tempdb''

    BEGIN

    SELECT ''?''

    ALTER DATABASE ? COLLATE XXXX

    END

    '

    Hi Everyone

    I cant get the code above to work without excluding the master database, Is there a way to exclude based on the DB ID, even though I have used "<>". it is still trying to pick up the master db.

    Thanks in advance.

  • EXEC master..sp_MSForeachdb '

    USE [?]

    IF DB_ID() > 4

    BEGIN

    SELECT ''?''

    ALTER DATABASE ? COLLATE XXXX

    END'

    Be careful if there's replication, as that will still affect the distribution DB.

    Also note that changing the collation of an existing database just affects the default collation of any tables and columns created after that. If does not change the collation of existing tables.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • still thesame, it tries to run on the master.

  • Are you sure? I just ran it on my local server (admittedly SQL 2008) with just the SELECT ''?'' inside the IF and I got a list of all of the non-system databases on the server. No sign of master, model, tempDB or MSDB.

    Take the alter database out and run it with just the select. Do you see the 4 system databases printed out?

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • yes, if it take off the alter statement. I dont see master, msdb etc.

    But with the alter statement in there, it tries to run it against master first.

    dont undertstand.

  • its a strange one this, i've just tried Gails SQL on both a 2005 and 2008 instance, without the ALTER statement it does not run on master, however with the ALTER statement in it attempts to run on Master.

    Strange!

  • Odd. I can't see why that should be happening.

    This seems to work, though I'm not sure why it does and the other version doesn't.

    EXEC master..sp_MSForeachdb '

    IF DB_ID(''?'') > 4

    BEGIN

    EXEC (''Alter Database ? COLLATE ... '')

    END'

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • So I say to the doctor, "Doc, when I hold my arm up like this, it hurts." Doctor says, "So don't do that."

    Being that this is SQL Server 2000 and VARCHAR(MAX) isn't available, rewrite this to use a regular cursor or While Loop instead of the monstrosity known as sp_MSForEachDB. It's one of the few places where a cursor or While Loop is condoned.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 8 posts - 1 through 7 (of 7 total)

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