Click to See Complete Forum and Search --> : To many databases


Lord Zarm
March 14th, 2003, 08:41 AM
I need a suggestion on what to do about the following problem.

I have 13 databases. I need to add 3 tables to each of these databases. Before I have been doing this manually. But this will take alot of time. Is there anything I can do to make this process alot easier. Soon I'll have 15 different databases. And hopefully more as each client has a databases. This already is quite alot to handle.

Thanks for any help or suggestion you might be able to give me.

M Owen
March 14th, 2003, 10:33 AM
What kind of databases? You can enumerate them and open connections to them and add your tables programmatically as long as the database allows it ...

emalagar
March 17th, 2003, 04:52 PM
Here is a solution for sql server 2000.
This solution utilizes an undocumented (but works great) stored procedure called :

sp_msforeachdb

Sample usage is:
EXEC sp_msforeachdb 'PRINT ''?'''



Suppose:
DECLARE @a_sql varchar(1000)
SET @a_sql = 'EXEC( USE ?
CREATE TABLE [dbo].[GPR] (
[GPRId] [smallint] IDENTITY (1, 1) NOT NULL ,
[GPRCode] [Rave_GPRCode] NOT NULL ,
[GPRDescription] [varchar] (64) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[ParentsGPRId] [smallint] NULL ,
[ConditionsId] [bigint] NULL ,
[GPRDepth] [tinyint] NULL
) ON [PRIMARY] )
'

Then you can execute this for all database:
EXEC sp_msforeachdb @a_sql


Please note: I have not personally tested the sample code - this reply just shows an idea of how to execute a an sql statement on all database in a sql server instance.