Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

sp_MSforeachtable:Add SQL objects to multiple database on a server.

By Mehernosh,

Add SQL objects to multiple database on a server.

If you wanted to add a SQL table or view or any SQL object to multiple database on your server and do not want to execute the script over and over again in all the multiple databases than the following SQL script can be used.

There have been instances where I had to add a stored procedure or a table in multiple client database as a part of the schema change process and this script has come in handy.


--ADD SQL OBJECTS(TABLES, VIEWS, SPS, UDFS ET AL) TO ALL THE DATABASES ON THE SERVER. 

PRINT '##### BEGIN DB SCRIPT ##### '
EXEC SP_MSFOREACHDB 
'USE [?]IF ''?'' NOT IN (''MASTER'', ''MODEL'', ''MSDB'', ''TEMPDB'', ''ADVENTUREWORKS'' )
BEGIN
IF NOT EXISTS
(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = ''MYTABLE'')
BEGIN
CREATE TABLE [DBO].[MYTABLE](COLUMN_1 VARCHAR(50) NULL, COLUMN_2 DATETIME NULL)
PRINT '' TABLE CREATED IN DATABASE ('' + DB_NAME() + '')''
END
ELSE
PRINT '' TABLE PRESENT IN DATABASE ('' + DB_NAME() + '')''
END '
PRINT '##### DB SCRIPT COMPLETED #####'

Please note that sp_MSforeachtable is undocumented/unsupported .

Even though you can use it in SQL server 2000, 2005, 2008 it might or might not be removed in the future versions.

So, use at your own risk and discretion.

Total article views: 541 | Views in the last 30 days: 2
 
Related Articles
ARTICLE

Manage Multiple SQL Server Installations and Databases with OSQL

This article explains how to use a simple batch file and script file to manage multiple databases on...

ARTICLE

Applying Your Scripts across Multiple SQL Server Databases

This review of RedGate's new SQL Multi Script tool that aids you in deploying a script across multip...

FORUM

database diagram print on one page

database diagram print

ARTICLE

Deploying Scripts To Multiple Instances

Most DBAs manage multiple SQL servers, each handling different needs for your business. Even with th...

FORUM

multiple database creation script

how to execute schema creation script and maintain them in a database ?

Tags
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones