SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 sysdatabases
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

stumbling
Posting Yak Master

104 Posts

Posted - 08/28/2007 :  06:45:16  Show Profile  Reply with Quote
Hi ALL

I have a server with 50 databases all the same with different data but the same structure, is there a way to update a table in multiple databases using the sysdatabases to get the relevant database names from?
I imagine it would have to be a cursor to do this? I checked thru the Search feature but could not find any info on this topic is it possible?

Cheers
Phil

AndrewMurphy
Flowing Fount of Yak Knowledge

Ireland
2916 Posts

Posted - 08/28/2007 :  06:57:32  Show Profile  Reply with Quote
you could use something like the system procedure "sp_msforeachdb"...search here for samples of it in use....and it's restrictions (unsupported i think)
Go to Top of Page

stumbling
Posting Yak Master

104 Posts

Posted - 08/28/2007 :  07:45:21  Show Profile  Reply with Quote
quote:
Originally posted by AndrewMurphy

you could use something like the system procedure "sp_msforeachdb"...search here for samples of it in use....and it's restrictions (unsupported i think)



Thanks for that you learn something everyday!!
I must be missing something but as the following will not run, any ideas?
Cheers
Phil

declare @sql nvarchar(4000)
set @sql = ''
select @sql = @sql + 'exec ' + name + '..sp_msforeachtable @command1= ''Update arm set code = ''cash1'' where code = ''cash''''' + char(13)
from master..sysdatabases
where name <> 'master'
and name <>'model'
and name <>'pubs'
and name <> 'msdb'
and name <> 'tempdb'
and name <>'northwind'
print @sql
exec (@sql)
Go to Top of Page

AndrewMurphy
Flowing Fount of Yak Knowledge

Ireland
2916 Posts

Posted - 08/28/2007 :  09:58:44  Show Profile  Reply with Quote
what do you get for the "print @SQL" line?
You can't say EXEC EXEC etc...."...which in effect is what the exec (@SQL) line states....
Go to Top of Page

AndrewMurphy
Flowing Fount of Yak Knowledge

Ireland
2916 Posts

Posted - 08/28/2007 :  10:00:06  Show Profile  Reply with Quote
Also....investigate using BEGIN TRANSACTION, COMMIT TRANSACTION statements....otherwise this could end up with you in a large pile of "dodo"
Go to Top of Page

rmiao
Flowing Fount of Yak Knowledge

USA
7266 Posts

Posted - 08/28/2007 :  20:34:30  Show Profile  Reply with Quote
Did you declare @command1?
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000