Solved

How to specify a select from a different database

Posted on 2011-03-20
3
291 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 69

Accepted Solution

by:
Qlemo earned 125 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 125 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

821 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