?
Solved

How to specify a select from a different database

Posted on 2011-03-20
3
Medium Priority
?
294 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 70

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

DFW AZURE MEETUP TONIGHT FRI 6PM

We will be discussing what Azure Stack is, how does it fit into the suit of offerings that Azure has currently, and where can it fit into your organizations technology stack. We will also be discussing limitations of the platform while covering various applicable scenarios.

Question has a verified solution.

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

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…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month12 days, 20 hours left to enroll

777 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