Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Joining across databases in one SQL statement?

2 views
Skip to first unread message

www.webtechy.co.uk

unread,
Jul 30, 2002, 6:52:26 AM7/30/02
to
How do I join two tables that are from two separate databases, but on the
same SQL server?

e.g. something along the lines of joining where db1.users.user_id =
db2.employees.employee_id in another database.

Hope you can help.

Regards,

Ben.

--
Business Advisor & IT Consultant
OPAR Ltd.


Tony Rogerson

unread,
Jul 30, 2002, 6:54:48 AM7/30/02
to
FROM db1.dbo.tblname t1
INNER JOIN db2.dbo.tblname t2 ON t2.id = t1.id

--
Tony Rogerson SQL Server MVP
Torver Computer Consultants Ltd
http://www.sql-server.co.uk [UK User Group, FAQ, KB's etc..]
http://www.sql-server.co.uk/tr [To Hire me]


"www.webtechy.co.uk" <newsg...@webtechy.co.uk> wrote in message
news:Veu19.4513$fw4.84843@newsfep2-gui...

Narayana Vyas Kondreddi

unread,
Jul 30, 2002, 7:03:45 AM7/30/02
to
Try this:

How to join tables from different databases?
http://vyaskn.tripod.com/programming_faq.htm#q13
--
HTH,
Vyas, MVP (SQL Server)
Check out my SQL Server website @
http://vyaskn.tripod.com/

"www.webtechy.co.uk" <newsg...@webtechy.co.uk> wrote in message
news:Veu19.4513$fw4.84843@newsfep2-gui...

Tom Moreau

unread,
Jul 30, 2002, 6:54:03 AM7/30/02
to
You use 3-part naming:

select
*
from
OtherDB.dbo.OtherTable o
join
MyDB.dbo.MyTable m on m.ID = o.ID

--
Tom

----------------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCT
SQL Server MVP
Columnist, SQL Server Professional

Toronto, ON Canada
www.pinnaclepublishing.com/sql
www.apress.com
---
www.webtechy.co.uk wrote in message ...

www.webtechy.co.uk

unread,
Jul 30, 2002, 8:01:07 AM7/30/02
to
Wow! Great (and quick) response, thanks guys, I shall try that out.

Regards,

Ben.

"Narayana Vyas Kondreddi" <answ...@hotmail.com> wrote in message
news:u4Ymrf7NCHA.2196@tkmsftngp08...

Tony Schleh

unread,
Jul 30, 2002, 2:56:57 PM7/30/02
to
Ben,

Use the 3 part naming convention.

The 2 Databases are: db1, db2

SELECT
c.*,
i.Invoice_Number
FROM
db1.dbo.Customer c
JOIN
db2.dbo.Invoice i
ON
c.CustomerID = i.CustomerID

"www.webtechy.co.uk" <newsg...@webtechy.co.uk> wrote in message news:<Veu19.4513$fw4.84843@newsfep2-gui>...

www.webtechy.co.uk

unread,
Aug 1, 2002, 6:45:31 PM8/1/02
to
Found out that the answer is to use the database owner in the reference.
i.e.

db1.dbo.users

Hope that helps someone else.

Cheers,

Ben.

"www.webtechy.co.uk" <newsg...@webtechy.co.uk> wrote in message
news:Veu19.4513$fw4.84843@newsfep2-gui...

0 new messages