July 1, 2009 at 8:25 am
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.
July 1, 2009 at 8:46 am
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
July 1, 2009 at 9:12 am
still thesame, it tries to run on the master.
July 1, 2009 at 9:24 am
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
July 1, 2009 at 10:04 am
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.
July 1, 2009 at 10:23 am
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!
July 1, 2009 at 10:27 am
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
July 19, 2009 at 11:56 pm
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
Change is inevitable... Change for the better is not.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply