Joining tables from different servers

后端 未结 4 997
我在风中等你
我在风中等你 2020-12-01 16:27

Any suggestions how to join tables from different servers in stored procedure?

4条回答
  •  执念已碎
    2020-12-01 17:08

    You have to first link two servers before joining the tables. Once they are linked, you can just use the below query and replace server, database & table names.

    Remember to execute the below sql in DB2:

    EXEC sp_addlinkedserver DB1
    GO
    
    -- below statement connects sa account of DB2 to DB1
    EXEC sp_addlinkedsrvlogin @rmtsrvname = 'DB1', @useself = 'false', @locallogin = 'sa', @rmtuser = 'sa', @rmtpassword = 'DB1 sa pwd'
    GO
    
    SELECT a.columns 
    FROM DB1.database_name.dbo.table_name a
    INNER JOIN DB2.database_name.dbo.table_name b
    ON a.columnId = b.columnId
    GO
    

    Linking servers - http://msdn.microsoft.com/en-us/library/ms188279.aspx

提交回复
热议问题