Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What is the Best way to

Status
Not open for further replies.

Kenos

Programmer
Jul 21, 2002
28
0
0
US
Hello Everyone


I have over 130 databases
that are exactly the same, (Different Data)

I need to drop a Column in a single table
table = Products
Column = Old_CAT_ID

Is there a way to create a stored PRoc to cycle all databases
and drop just one Column

Thanks
 
There's an undocumented Stored Procedure that can help:

Procedure:
sp_MSforeachdb
@command1
@replacechar = '?'
[,@command2]
[,command3]
[,@whereand]
[,@precommand]
[,@postcommand]

Purpose:
execute up to three commands for every database on the system. @replacechar will be replaced with the name of each database. @precommand and @postcommand can be used to direct commands to a single result set.

Example:
EXEC sp_MSforeachdb @command1 = 'Print "Listing ?=', @command2 = 'SELECT * FORM ?', @whereand = ' AND name like "title%"'

-From the Gurus's Guide to Transact-SQL by Ken Henderson

-SQLBill
 
If all the DB's are on server, you can write this query in Master and then take the results and post into another query window and exec it:

set nocount on

select 'use ' + name + '
GO ' + '
alter table Products drop column Old_CAT_ID
GO'
from sysdatabases
 
Using sp_MSforeachdb it would be:

EXEC sp_MSforeachdb @command1 = 'USE ? ALTER TABLE Products DROP COLUMN Old_CAT_ID'

(BTW-In the example in my previous post FORM should have been FROM)

-SQLBill
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top