Selecting data from two different servers in SQL Server

后端 未结 15 1692

How can I select data in the same query from two different databases that are on two different servers in SQL Server?

相关标签:
15条回答
  • 2020-11-22 08:13

    Created a Linked Server definition in one server to the other (you need SA to do this), then just reference them with 4-part naming (see BOL).

    0 讨论(0)
  • 2020-11-22 08:16
    SELECT
            *
    FROM
            [SERVER2NAME].[THEDB].[THEOWNER].[THETABLE]
    

    You can also look at using Linked Servers. Linked servers can be other types of data sources too such as DB2 platforms. This is one method for trying to access DB2 from a SQL Server TSQL or Sproc call...

    0 讨论(0)
  • 2020-11-22 08:17

    As @Super9 told about OPENDATASOURCE using SQL Server Authentication with data provider SQLOLEDB . I am just posting here a code snippet for one table is in the current sever database where the code is running and another in other server '192.166.41.123'

    SELECT top 2 * from dbo.tblHamdoonSoft  tbl1 inner JOIN  
    OpenDataSource('SQLOLEDB','Data Source=192.166.41.123;User ID=sa;Password=hamdoonsoft')
    .[TestDatabase].[dbo].[tblHamdoonSoft1] tbl2 on tbl1.id = tbl2.id
    
    0 讨论(0)
提交回复
热议问题