So I was faced with a problem the other day where I had to map data from one datasource to another (well, common problem) but both of the sources were SQL Servers, and I had to map the data based on some system ids and names, so I couldn’t just move all the data between the sources.
I then looked up the linked servers and was surprised at how easy that was.

Basically – all I needed to do what set up a linked server by using the following system stored procedures:
sp_addlinkedserver '<Server Alias>', '', 'SQLNCLI', NULL, NULL, 'SERVER=<IP>', NULL

sp_addlinkedsrvlogin '<Server Alias>', 'false', NULL, '<username>', '<password>'

The first call sets up the server, whereas the second one sets up a log in to the server.

Be aware that the traffic between the servers as far as I know are sent pretty plain text/not encrypted, so I would not advice this to run for a prolonged time in a product environment lest it is located behind some VPN or similar.

To remove the linked server once done then just call

sp_dropserver '<Server Alias>', 'droplogins';

Linking servers means you can access a different database server with a qualified name, as if it was on the same database server.

If I for instance set up a linked server called myRemoteServer, then I would be able to access a table in the remote database using:
myRemoteServer.<DatabaseName>.<SchemaName>.<TableName>

It makes it relative much easier to map data between sources compared to having to backup/restore the remote source database to the target server.

5 Responses to “Linked Servers in SQL Server 2005”

  1. I have linked 2005 machines together. I have a SQL2000 machine in our environment. I cannot get a SQL2005 server to link it.

  2. I have personally not had any problems linking to a 2000 server from a 2005, by using the above method.
    Are you sure there is access between the machines?

  3. I can’t use the user defined function , how to obtain that

  4. Can you link a SQL express server and SQL? I tried with no luck.

  5. Sql Server doesn’t allow remote connections????

    I seem to create the ‘link’ fine. And also the ‘user login’… but when I try to view some data… I get:

    OLE DB provider "SQLNCLI" for linked server "REMOTE_SERVER" returned message "Login timeout expired".
    OLE DB provider "SQLNCLI" for linked server "REMOTE_SERVER" returned message "An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections.".
    Msg 87, Level 16, State 1, Line 0
    SQL Network Interfaces: Connection string is not valid [87].

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>