Need an SQL query to connect the two tables in the different databases but in the same server.
Asked
Active
Viewed 213 times
0
-
3a little search could have given you a solution. [search result](http://stackoverflow.com/search?q=sql+query+on+multiple+databases) ... ( like [this answer](http://stackoverflow.com/questions/5931652/sql-query-on-multiple-databases) ) – KarelG Feb 13 '14 at 07:47
-
1just prefix your tables correctly (`
. – Raphaël Althaus Feb 13 '14 at 07:48. `)
3 Answers
0
select * from Databasename1.dbo.tablename1 A
inner join Databasename2.dbo.tablename1 B
on A.ID=B.ID
or
select A.*,B.* from Databasename1.dbo.tablename1 A
inner join Databasename2.dbo.tablename1 B
on A.ID=B.ID
or
select A.columnname1,B.columnname2 from Databasename1.dbo.tablename1 A
inner join Databasename2.dbo.tablename1 B
on A.ID=B.ID

Vikram Jain
- 5,498
- 1
- 19
- 31
0
Try this :
'SELECT a.userID, b.usersFirstName, b.usersLastName FROM databaseA.dbo.TableA a inner join database B.dbo.TableB b ON a.userID=b.userID'
[Reference] (http://forums.asp.net/t/1254974.aspx?How+to+join+tables+from+different+databases+in+SQL+select+statement+)

Eldho
- 7,795
- 5
- 40
- 77
-
Thanks Eldho the query works,and i also need the query to connect the tables and database that are in different server – user3057765 Feb 13 '14 at 08:57
-
User try this [link] (http://stackoverflow.com/questions/5145637/querying-data-by-joining-two-tables-in-two-database-on-different-servers) – Eldho Feb 13 '14 at 10:13
0
select D1.Field1, D2.Field2
from Database1.dbo.TableofD1 D1
inner join Database2.dbo.TableofD2 D2
on D1.fieldmain=D2.fieldmain

sankoobaba
- 180
- 4
- 12