Selecting data from two different servers in SQL Server

后端 未结 15 1725

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:07

    I had same issue to connect an SQL_server 2008 to an SQL_server 2016 hosted in a remote server. Other answers didn't worked for me straightforward. I write my tweaked solution here as I think it may be useful for someone else.

    An extended answer for remote IP db connections:

    Step 1: link servers

    EXEC sp_addlinkedserver @server='SRV_NAME',
       @srvproduct=N'',
       @provider=N'SQLNCLI',   
       @datasrc=N'aaa.bbb.ccc.ffffd';
    
    EXEC sp_addlinkedsrvlogin 'SRV_NAME', 'false', NULL, 'your_remote_db_login_user', 'your_remote_db_login_password'
    

    ...where SRV_NAME is an invented name. We will use it to refer to the remote server from our queries. aaa.bbb.ccc.ffffd is the ip address of the remote server hosting your SQLserver DB.

    Step 2: Run your queries For instance:

    SELECT * FROM [SRV_NAME].your_remote_db_name.dbo.your_table
    

    ...and that's it!

    Syntax details: sp_addlinkedserver and sp_addlinkedsrvlogin

提交回复
热议问题