Solved

How to specify a select from a different database

Posted on 2011-03-20
3
289 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 68

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now