423

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

RBarryYoung
  • 55,398
  • 14
  • 96
  • 137
  • 8
    The answers from Eric and Raging Bull very handy. I was able to use this to copy mass volumes of data from DEV to PROD cutting down times from ranged from 5 hours to 18 hours, down to 17 seconds. – Chris Aldrich May 29 '15 at 15:37
  • @Eric, kudos for editing a marginally ambiguous question and making it a 170-rep question :) – Eric Wu Aug 08 '16 at 17:34

16 Answers16

379

What you are looking for are Linked Servers. You can get to them in SSMS from the following location in the tree of the Object Explorer:

Server Objects-->Linked Servers

or you can use sp_addlinkedserver.

You only have to set up one. Once you have that, you can call a table on the other server like so:

select
    *
from
    LocalTable,
    [OtherServerName].[OtherDB].[dbo].[OtherTable]

Note that the owner isn't always dbo, so make sure to replace it with whatever schema you use.

mark_h
  • 5,233
  • 4
  • 36
  • 52
Eric
  • 92,005
  • 12
  • 114
  • 115
111

You can do it using Linked Server.

Typically linked servers are configured to enable the Database Engine to execute a Transact-SQL statement that includes tables in another instance of SQL Server, or another database product such as Oracle. Many types OLE DB data sources can be configured as linked servers, including Microsoft Access and Excel.

Linked servers offer the following advantages:

  • The ability to access data from outside of SQL Server.
  • The ability to issue distributed queries, updates, commands, and transactions on heterogeneous data sources across the enterprise.
  • The ability to address diverse data sources similarly.

Read more about Linked Servers.

Follow these steps to create a Linked Server:

  1. Server Objects -> Linked Servers -> New Linked Server

  2. Provide Remote Server Name.

  3. Select Remote Server Type (SQL Server or Other).

  4. Select Security -> Be made using this security context and provide login and password of remote server.

  5. Click OK and you are done !!

Here is a simple tutorial for creating a linked server.

OR

You can add linked server using query.

Syntax:

sp_addlinkedserver [ @server= ] 'server' [ , [ @srvproduct= ] 'product_name' ] 
     [ , [ @provider= ] 'provider_name' ]
     [ , [ @datasrc= ] 'data_source' ] 
     [ , [ @location= ] 'location' ] 
     [ , [ @provstr= ] 'provider_string' ] 
     [ , [ @catalog= ] 'catalog' ] 

Read more about sp_addlinkedserver.

You have to create linked server only once. After creating linked server, we can query it as follows:

select * from LinkedServerName.DatabaseName.OwnerName.TableName
Raging Bull
  • 18,593
  • 13
  • 50
  • 55
  • 1
    Note: see [here](http://alexpinsker.blogspot.co.uk/2007/08/how-to-give-alias-to-sql-linked-server.html) for how to have server name be something other than the hostname/port. – Richard Feb 11 '15 at 10:07
  • 2
    Bit of a tip, here if you are having trouble with the sp_addlinkedserver. Create the server in the dialog - make sure it works - then right click the connection and select the scrip[t linked server AS create – Richard Housham Aug 19 '16 at 09:59
27
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...

RSolberg
  • 26,821
  • 23
  • 116
  • 160
  • 2
    will this method work all the time ? what are the scenarios where it could fail ? – Steam Jan 14 '14 at 21:50
  • 4
    Confirmed this fails in my env, error says I needed to use addlinkedserver – gorlaz Mar 14 '17 at 03:32
  • 1
    Does this work for anyone, without using a Linked Server? – Doug S Feb 13 '18 at 00:34
  • tested and error received is `Could not find server '88.208.229.164' in sys.servers. Verify that the correct server name was specified. If necessary, execute the stored procedure sp_addlinkedserver to add the server to sys.servers. ` – WhatsThePoint Apr 11 '18 at 10:07
25

Querying across 2 different databases is a distributed query. Here is a list of some techniques plus the pros and cons:

  1. Linked servers: Provide access to a wider variety of data sources than SQL Server replication provides
  2. Linked servers: Connect with data sources that replication does not support or which require ad hoc access
  3. Linked servers: Perform better than OPENDATASOURCE or OPENROWSET
  4. OPENDATASOURCE and OPENROWSET functions: Convenient for retrieving data from data sources on an ad hoc basis. OPENROWSET has BULK facilities as well that may/may not require a format file which might be fiddley
  5. OPENQUERY: Doesn't support variables
  6. All are T-SQL solutions. Relatively easy to implement and set up
  7. All are dependent on connection between source and destionation which might affect performance and scalability
Tim
  • 1,755
  • 2
  • 22
  • 34
super9
  • 29,181
  • 39
  • 119
  • 172
25

These are all fine answers, but this one is missing and it has it's own powerful uses. Possibly it doesn't fit what the OP wanted, but the question was vague and I feel others may find their way here. Basically you can use 1 window to simultaneously run a query against multiple servers, here's how:

In SSMS open Registered Servers and create a New Server Group under Local Server Groups.

Under this group create New Server Registration for each server you wish to query. If the DB names are different ensure to set a default for each in the properties.

Now go back to the Group you created in the first step, right click and select New Query. A new query window will open and any query you run will be executed on each server in the group. The results are presented in a single data set with an extra column name indicating which server the record came from. If you use the status bar you will note the server name is replaced with multiple.

Paul
  • 1,041
  • 11
  • 26
  • 4
    This seems to assume that the query uses the same tables on all databases. (Which is fine for standard tables like sys.tables but not likely for custom made tables like dbo.mycustomers) – Dennis Jaheruddin Nov 29 '17 at 12:57
  • Given it's "the same query from two different databases" it's highly likely to have the same tables. But yes I routinely use this method for a production system housed on several servers and for querying MSDB tables. – Paul Nov 29 '17 at 13:30
  • Really cool feature actually. The drawback is the schema of the result set must match, since it executes the query twice and merges them all at the same time. It would be great if you could reference the servers within the SQL itself, like you can with linked servers, even if you couldn't JOIN result set and the sets had to be constructed to be evaluated separately. – Kross May 24 '19 at 13:34
  • 1
    @Kross you sort of could. Create an #output table, do logic based on @@SERVERNAME and populate data into #output then end it with a select on that. I did a similar thing for querying Log info from a mix of SQL2000 and SQL2008R2 machines which had different levels/columns of information, but instead of @@SERVERNAME I was using a server version variable. – Paul May 29 '19 at 14:41
  • 1
    This is a lovely Answer, and a year later someone else was looking just for this. Thank you –  Jul 09 '20 at 19:43
18

try this:

SELECT * FROM OPENROWSET('SQLNCLI', 'Server=YOUR SERVER;Trusted_Connection=yes;','SELECT * FROM Table1') AS a
UNION
SELECT * FROM OPENROWSET('SQLNCLI', 'Server=ANOTHER SERVER;Trusted_Connection=yes;','SELECT * FROM Table1') AS a
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
Anna Karthi
  • 273
  • 3
  • 2
14

I had the 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.ddd';
   
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.ddd 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

MarcM
  • 2,173
  • 22
  • 32
6

Server 2008:

When in SSMS connected to server1.DB1 and try:

SELECT  * FROM
[server2].[DB2].[dbo].[table1]

as others noted, if it doesn't work it's because the server isn't linked.

I get the error:

Could not find server DB2 in sys.servers. Verify that the correct server name was specified. If necessary, execute stored procedure sp_addlinkedserver to add the server to sys.servers.

To add the server:

reference: To add server using sp_addlinkedserver Link: [1]: To add server using sp_addlinkedserver

To see what is in your sys.servers just query it:

SELECT * FROM [sys].[servers]
ChrisM
  • 1,576
  • 6
  • 18
  • 29
user3586922
  • 59
  • 1
  • 3
6

Simplified solution for adding linked servers

First server

EXEC sp_addlinkedserver @server='ip,port\instancename'

Second Login

EXEC sp_addlinkedsrvlogin 'ip,port\instancename', 'false', NULL, 'remote_db_loginname', 'remote_db_pass'

Execute queries from linked to local db

INSERT INTO Tbl (Col1, Col2, Col3)
SELECT Col1, Col2, Col3
FROM [ip,port\instancename].[linkedDBName].[linkedTblSchema].[linkedTblName]
irfandar
  • 1,690
  • 1
  • 23
  • 24
4

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).

RBarryYoung
  • 55,398
  • 14
  • 96
  • 137
4
 select * 
 from [ServerName(IP)].[DatabaseName].[dbo].[TableName]
mattmanser
  • 5,719
  • 3
  • 38
  • 50
Masum
  • 119
  • 11
3

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
Muhammad Ashikuzzaman
  • 3,075
  • 6
  • 29
  • 53
1

I know this is an old question but I use synonyms. Supposedly the query is executed within database server A, and looks for a table in a database server B that does not exist on server A. Add then a synonym on A database that calls your table from server B. Your query doesn't have to include any schemas, or different database names, just call the table name per usual and it will work.

There's no need to link servers as synonyms per say are sort of linking.

  • 1
    Now then, what is a "synonym" in this context? – Oskar Berggren May 07 '18 at 13:59
  • It's a database object that refers to a base object in another database. More info here: https://learn.microsoft.com/en-us/sql/relational-databases/synonyms/synonyms-database-engine?view=sql-server-2017 – Niklas Henricson May 08 '18 at 00:20
  • 1
    Cool, I did not know about that feature. However, you also state they avoid the need for a linked server, but I fail to see how. Synonyms itself appear to be just that, a synonym, and not itself contain any specific remoting ability. In example B at https://learn.microsoft.com/en-us/sql/t-sql/statements/create-synonym-transact-sql?view=sql-server-2017, they create a linked server before referencing it from a synonym. – Oskar Berggren May 09 '18 at 07:23
  • True, I assumed the databases are in the same server environment. Of course you'll always have to link databases if they're remote from each other. There's no other way to access with a database-to-database relationship. – Niklas Henricson May 09 '18 at 13:07
0
sp_addlinkedserver('servername')

so its should go like this -

select * from table1
unionall
select * from [server1].[database].[dbo].[table1]
Andrius Naruševičius
  • 8,348
  • 7
  • 49
  • 78
ugio
  • 39
  • 1
  • 1
  • 4
0

Server Objects---> linked server ---> new linked server

In linked server write server name or IP address for other server and choose SQL Server In Security select (be made using this security context ) Write login and password for other server

Now connected then use

Select * from [server name or ip addresses ].databasename.dbo.tblname
ChrisM
  • 1,576
  • 6
  • 18
  • 29
Sameh
  • 19
0

I hope the clarifications all mentioned above, have answered the OP's original question. I just want to add a code snippet for adding SQL Server as a linked server.

At most basic, we can simply add SQL Server as a linked server by executing sp_addlinkedserver with only one parameter @server, i.e.

-- using IP address
exec sp_addlinkedserver @server='192.168.1.11' 
-- PC domain name 
exec sp_addlinkedserver @server='DESKTOP-P5V8JTN'

SQL Server will automatically fill SRV_PROVIDERNAME, SRV_PRODUCT, SRV_DATASOURCE etc. with default values. By doing this, we've to write the IP or PC domain name in the 4 part table address in the query (example below). This can be more annoying or less readable when the linked server will not have a default port or instance, the address will look similar to this 192.168.1.11,1430 or 192.168.1.11,1430\MSSQLSERVER2019.

So, to keep 4 part address short and readable, we can add an alias name for the server instead of the full address by specifying other parameters as follows-

exec sp_addlinkedserver
    @server='ReadSrv1',
    @srvproduct='SQL Server',
    @provider='SQLNCLI',
    @datasrc='192.168.1.11,1430\MSSQLSERVER2019'

But when you'll execute the query, the following error will show- You cannot specify a provider or any properties for product 'SQL Server'. If we keep the server product property value empty '' or any other value, the query will execute successfully.

Next step, make login to the remote linked server by executing the following query-

EXEC sp_addlinkedsrvlogin @rmtsrvname = 'ReadSrv1', @useself = 'false', @locallogin = NULL, @rmtuser = 'sa', @rmtpassword = 'LinkedServerPasswordForSA'

Finally, use the linked server with 4 part address, the syntax is-
[ServerName].[DatabaseName].[Schema].[ObjectName]
Example-

SELECT TOP 100 t.* FROM ReadSrv1.AppDB.dbo.ExceptionLog t
  • To list existing linked servers execute:
    exec sp_linkedservers
  • To delete a linked server execute:
    exec sp_dropserver @server = 'ReadSrv1', @droplogins='droplogins' (delete login as well) OR
    exec sp_dropserver @server = 'ReadSrv1', @droplogins='NULL' (keep login)
quasar
  • 404
  • 6
  • 17