Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a .sql file which has many create table statements.But some of them are needed to be executed on one database ex:-DB1 and some on another ex:-DB2.how can i specify names of databases so that query gets fired on specified database.

I have found two options
create table DB1.dbo.Tablename(ID INT)
create table DB2.dbo.Tablename(ID INT)

and

use DB1
GO
create table dbo.Tablename(ID INT)
GO

Is there any other way so that i can assign the name of particular database to a bunch of queries once and queries get executed on that particular database?
Posted
Comments
Richard MacCutchan 13-Jun-15 4:31am    
You select the database in your connection string.
Correct. :)
CHill60 13-Jun-15 9:21am    
I presume you are running these in SQL Server Management Studio? If not then how are you running the scripts?
What is wrong with using use DB1 followed by all of the commands to be run against that database, followed by use DB2, followed by all of the commands to be run against the 2nd database? It works (I've done it myself several times for implementations)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900