If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > Querying tables from different databases on same sql server

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old
Registered User
 
Join Date: Sep 2003
Posts: 176
Querying tables from different databases on same sql server

Hi all,

How do I query two tables in different databases on the same SQL Server?

In short, I want to do:

Select A.*
from database 1. table 1 as A
inner join database 2. table 1 as B
on A.COL1 = B.COL1

Both database 1 and 2 are on the same SQL Server.

Please advise.

Thanks,

V
Reply With Quote
  #2 (permalink)  
Old
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 12,561
You just need to fully qualify the object names:

select * from Database.Owner.Table

...but the owner is assumed to be dbo if omitted, so this works most of the time:

select * from Database..Table
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
www.LobsterShot.blogspot.com
Reply With Quote
  #3 (permalink)  
Old
Registered User
 
Join Date: Aug 2006
Location: San Francisco, CA
Posts: 136
Huh...I didn't know this. I guess you learn something new everyday.

Thanks blindman
Reply With Quote
  #4 (permalink)  
Old
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 14,820
Quote:
Originally Posted by Reghardt
Huh...I didn't know this. I guess you learn something new everyday.
Pretty much any day that is worth getting out of bed anyway!

-PatP
Reply With Quote
  #5 (permalink)  
Old
another indirection layer
 
Join Date: May 2004
Location: Seattle
Posts: 1,313
you also forget something every day. trick is to forget the useless stuff.
__________________
elsasoft.org
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On