Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Data from Two different Databases

Author  Topic 

wisdomtree2009
Starting Member

5 Posts

Posted - 2009-01-08 : 01:30:20
Hi

I have two tables A and B with common column c1. these two tables are in two databases db1 and db2. How can i retrieve the data by joining the two tables.

Any help Appreciated

Jai Krishna
Constraint Violating Yak Guru

333 Posts

Posted - 2009-01-08 : 01:35:18
SELECT * FROM
databasename1..tablename1 t1
inner join databasename2..tablename2 t2 on (t1.commoncol =t2.commoncol)

Jai Krishna
Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-01-08 : 01:36:10
select * from db1.taba as a
inner join db2.tabb as b on ( a.c1 = b.c1)
Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-01-08 : 01:49:09
using schema,
select * from Databasename.Schemaname.table1 AS t1
INNER JOIN Databasename.Schemaname.table2 AS t2 ON (t1.c1 = t2.c1)

without using schema,
select * from Databasename..table1 AS t1
INNER JOIN Databasename..table2 AS t2 ON (t1.c1 = t2.c1)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-01-08 : 01:50:50
Assuming dbs are on same server all above solutions will work. Just in case, they are on different server, you need to configure linked server b/w them or use OPENROWSET()
Go to Top of Page
   

- Advertisement -