How to specify a select from a different database

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


ColumnName FROM Database2.TableName


Invalid object name 'Database2'

What is the correct syntax ?

Who is Participating?
QlemoConnect With a Mentor Batchelor and DeveloperCommented:
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.
jimyXConnect With a Mentor Commented:
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
DovbermanAuthor Commented:
Thank you. Works perfectly.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.