?
Solved

How to specify a select from a different database

Posted on 2011-03-20
3
Medium Priority
?
302 Views
Last Modified: 2012-05-11

I have two databases, Database1 and Database2 on the same server.

I need to select a column form Database1 and a column from Database2

The query is being built in Database1

SELECT

ColumnName FROM Database2.TableName

error:

Invalid object name 'Database2'

What is the correct syntax ?

Thanks,
 
0
Comment
Question by:Dovberman
3 Comments
 
LVL 71

Accepted Solution

by:
Qlemo earned 500 total points
ID: 35175542
You are talking about MSSQL, I suppose.
A full path to an object contains of four parts :    server.database.schema.object
If the parts on the left are unchanged from your current location, you can leave them off. On the same server you only need the three right parts:
    SELECT ColumnName FROM Database2.dbo.TableName

dbo can be skipped in most cases, but it is better to provide it. dbo is the "generic" database owner, BTW, so it does not matter which account has been really used to create the DB.
0
 
LVL 24

Assisted Solution

by:jimyX
jimyX earned 500 total points
ID: 35175543
You need to do this:

SELECT T1.ColumnName
FROM TableName T1

And if you want to select two column from two tables then:

SELECT T1.ColumnName, T2.ColumnName
FROM Table1Name T1, Table2Name T2
0
 

Author Closing Comment

by:Dovberman
ID: 35175664
Thank you. Works perfectly.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Loops Section Overview
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question