需要一个SQL查询来连接两个不同数据库中的表

时间:2014-02-13 07:44:26

标签: sql-server-2008

需要SQL查询来连接不同数据库中但位于同一服务器中的两个表。

3 个答案:

答案 0 :(得分:0)

select * from Databasename1.dbo.tablename1 A
inner join  Databasename2.dbo.tablename1 B
on A.ID=B.ID

 select A.*,B.* from Databasename1.dbo.tablename1 A
    inner join  Databasename2.dbo.tablename1 B
    on A.ID=B.ID

 select A.columnname1,B.columnname2 from Databasename1.dbo.tablename1 A
inner join  Databasename2.dbo.tablename1 B
on A.ID=B.ID

答案 1 :(得分:0)

试试这个:

 'SELECT a.userID, b.usersFirstName, b.usersLastName  FROM databaseA.dbo.TableA a inner join database B.dbo.TableB b  ON a.userID=b.userID'  

[参考](http://forums.asp.net/t/1254974.aspx?How+to+join+tables+from+different+databases+in+SQL+select+statement+

[参考文献2](http://social.msdn.microsoft.com/Forums/en-US/1e6d7732-aa67-4069-b373-d7220dc251aa/merge-two-tables-from-different-databases?forum=vblanguage

答案 2 :(得分:0)

  

选择   D1.Field1,   D2.Field2

     

来自Database1.dbo.TableofD1 D1

     

内   加入Database2.dbo.TableofD2 D2
  在D1.fieldmain = D2.fieldmain