views:

218

answers:

2

I wrote some sql scripts to create a database and store data. I just noticed that the new tables and data are going to the master database.

I found that I can address the correct database if I scope out the database as so:

CREATE TABLE Practice1.dbo.Experiments
(   
    ID int IDENTITY (100,1) PRIMARY KEY,  
    CompanyName nvarchar (50)        
)

but I'd rather not have to scope out each command. Is there a way to set the database in the script so I don't have to scope everything out?

INSERT INTO Practice1.dbo.EXPERIMENTS
VALUES 
(
'hello world'
)


SELECT * FROM Practice1.dbo.EXPERIMENTS
+4  A: 

You have a drop down list on your toolbar that allows you to select what database you want the script to execute on. Also, you can state the database to use at the top of your script.

Example:

Syntax

USE {database}

http://doc.ddart.net/mssql/sql70/ua-uz_7.htm

TheTXI
A: 

On SQL Server 2005, to switch the database context, use the command:

USE DatabaseName

in the samples above, the database name is Practice1, hence:

USE Practice1

MedicineMan