2

This should be a relatively simple thing to do. Basically I have SQL Server 2008, and I have attached NorthWind database to it. This is my query which works fine.

SELECT [Customers].[CompanyName], [Orders].[OrderID]
FROM [Northwind].[dbo].[Customers]
LEFT JOIN [Northwind].[dbo].[Orders]
ON Orders.CustomerID=Customers.CustomerID
ORDER BY Customers.CompanyName; 

In the second SQL line, how can I go about being able to do

FROM [Customers] 

Instead of

FROM [Northwind].[dbo].[Customers]

Is it possible? or is it not the right way to do it? I have 3 more databases attached to SQL Server by the way and NorthWindis not the only one. I googled it but I am not quite sure what terms to google. I just want to shorten my queries. Thank you

1
  • Please note that it is a good practice to always use schema.tableName instead of just tableName. So even if FROM [Customers] will work when you write 'USE Northwind', you should still always use [dbo].[Customers] instead. May 16, 2013 at 12:17

3 Answers 3

6

At the beginning of your query indicate what database you are using.

USE [Northwind]
3

There are a few ways that you can do this.

When you open a new query window in SSMS you can add the following line to the top:

    USE Northwind

Or when you open a new query window you can change the database in the dropdown menu on the SQL Editor toolbar to Northwind, see screenshot:

enter image description here

Finally, when working in the Northwind data base, if you open a new query window, it should default to that database.

1
  • Thanks.Accepted and +1 for your effort for doing [PrintScreen] for the picture illustration. :D May 16, 2013 at 12:02
1

Add Use [Northwind] before the query

OR

Select the Northwind database from the database dropdown, if using SSMS

OR

Start a new query window by right-clicking on the Northwind database and selecting new query option

(All are the one and the same thing)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.