Joining tables from different servers

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

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

相关标签:
4条回答
  • 2020-12-01 16:55

    Without more details, it's hard to give direct examples, but here is the basic idea:

    First, outside of the stored procedure, the host server (the server the stored procedure will be on) has to know about the second server, including (possibly) login information.

    On your main server, run the sp_addlinkedserver stored procedure. This only has to be done once:

    exec sp_addlinkedserver @server='(your second server)';
    

    If you need to provide login information to this second server (for example, the process can't log in with the same credentials that are used in the initial database connection), do so with the sp_addlinkedsrvlogin stored proc:

    exec sp_addlinkedsrvlogin @rmtsrvname='(your second server)',
    @useself=false, 
    @rmtuser='yourusername', 
    @rmtpassword='yourpassword';
    

    Then, in your stored procedure, you can specify tables on the second server:

    SELECT table1.*
    FROM table1
    INNER JOIN [secondserver].[database].[schema].[table] AS table2 ON
        table1.joinfield = table2.joinfield
    
    0 讨论(0)
  • 2020-12-01 17:05

    1. Check to see if you have any linked servers using exec sp_helpserver

    2. If your server is not returned then it is not Linked meaning you will need to add it. Otherwise move to step 3.

    For Sql Server 2008 R2, go to Server Object > Linked Servers > Add new Linked Server

    Or

    exec sp_addlinkedserver @server='ServerName'; 
    

    3. Connect to the Secondary server like so...

    exec sp_addlinkedsrvlogin 
    @rmtsrvname='ServerName'
    , @useself=false
    , @rmtuser='user'
    , @rmtpassword='Password';
    

    4. Now you can Join the tables for the two different servers.

    SELECT 
        SRV1.*
    FROM 
        DB1.database_name.dbo.table_name SRV1
            INNER JOIN DB2.database_name.dbo.table_name SRV2
            ON SRV1.columnId = SRV2.columnId
    GO
    
    0 讨论(0)
  • 2020-12-01 17:05

    You can write query as below syntax to join other server in SQL Server

    SELECT table_1.*  
    FROM [Database_1].[dbo].[Table_1] table_1
    INNER JOIN  [IP_OF_SERVER_2].[Database_2].[dbo].[Table_2] table_2 ON table_1.tablekey COLLATE DATABASE_DEFAULT  = table_2.tablekey COLLATE DATABASE_DEFAULT
    

    p/s: COLLATE DATABASE_DEFAULT to encode, Prevent bellow error Cannot resolve the collation conflict between "Vietnamese_CI_AS" and "SQL_Latin1_General_CP1_CI_AS" in the equal to operation.

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题