• subscribe
June 16, 2000 10:10 AM

Linked Servers

SQL Server Pro
InstantDoc ID #8992
Downloads
8992.zip

Query remote SQL Server and non-SQL Server databases as if they were local

Linked servers are often-untapped resources that let you use distributed queries to query any server as if it were local. Distributed queries let you keep your inventory data in a DB2 database, for example, and your accounting data in a SQL Server database and query both sets of data as if they were in the same database, eliminating the cost and hassle of merging the two systems. SQL Server lets you execute such queries on any OLE DB-compliant data source. This article shows you how to set up, query, and gather meta data about linked servers.

About Linked Servers
Linked servers are descendants of remote servers, which you can use to execute replication stored procedures in SQL Server. However, Microsoft recommends that you use linked servers instead of remote servers in SQL Server 2000 or SQL Server 7.0 because linked servers have much more functionality, such as the ability to run ad hoc queries. With linked servers, you begin by establishing a connection in SQL Server to a compatible remote OLE DB provider. SQL Server clients connect to the linked server, then SQL Server connects to the remote provider on the client's behalf, as Figure 1 shows. The linked server acts as a middleman, taking an order from the consumer, passing it to the source, then passing it back to the consumer.

Linked servers are especially useful when configuring the OLE DB data source on each client computer is too time-consuming. In addition, stabilizing connections to other types of servers, such as DB2, can be tricky; linked servers minimize this complexity because you need to configure the connection only once. Clients need to connect only to the standard SQL Server provider; they don't need to have an OLE DB provider for DB2, for example, on their workstations.

Linked servers are also the core technology in SQL Server 2000 distributed partitioned views. In a distributed partitioned view, you can make several uniformly distributed tables appear as one table and distribute the load of large queries among many servers. (For more information about distributed partitioned views, see Kalen Delaney and Itzik Ben-Gan, "Distributed Partitioned Views," page 32.) To make this new SQL Server 2000 feature work, however, you need to inform each node of the other nodes' existence by adding a linked server for each participating node in each SQL Server system.

Setting Up a Linked Server
Most of the following examples show you how to link one SQL Server machine to another. I also explain briefly how to connect to DB2 and Oracle. Linked servers are new beginning in SQL Server 7.0, so avoid bugs by installing the latest service pack. If you haven't already done so, I recommend that you install Service Pack 2 (SP2), which contains quite a few fixes for linked servers. Some of the most dangerous bugs include access violation (AV) errors that occur when you use linked servers. For more information about AVs, see the Microsoft articles "FIX: Queries Using Linked Servers May Cause Access Violation and CPU Spin" (http://support.microsoft .com/support/kb/articles/q246/0/31.asp) and "FIX: Insert from Linked Server Into Table with BIT Field Having a DEFAULT Constraint Causes AV" (http://support.microsoft.com/support/kb/articles/q240/7/98.asp).

You can add a linked server through Enterprise Manager. From the Security menu, right-click the linked server icon and select New Linked Server. Or you can script the process in T-SQL by using the sp_addlinkedserver stored procedure:

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

Table 1, page 42, describes each of these parameters, which also exist in the Enterprise Manager screens. The following sp_addlinkedserver example adds a linked server named LINKEDSERVER, which connects to a SQL Server named BKNIGHT:

EXEC sp_addlinkedserver
   @server=LINKEDSERVER,
   @srvproduct = 'SQLServer OLEDB Provider',
   @provider = 'SQLOLEDB',
   @datasrc = 'BKNIGHT'


ARTICLE TOOLS

Comments
  • Sage
    6 years ago
    Dec 28, 2006

    for the person looking to see if a linked server exists, you can check the output of
    sp_LinkedServers,

    or you can do what the sp_linkedServers procedure does and run the following

    select *
    from master.dbo.sysservers
    Where srvname = @YourLInkedServerName

  • Anonymous User
    8 years ago
    Nov 23, 2004

    My shop recently installed a packed application which uses SQL Server 2000 as the database and UDB on AIX as the enterprise database to replicate data to and from. We use the Linked server with IBM OLE DB provider to access the enterprise data.

    The vendor for the package application recommends us to enable "ALLOW INPROCESS" option on the linked server.

    On the daily basis we run two batch jobs to upload and download (replicate) data between the two heterogenous databases. However, we are experiencing memory problem after a few runs of the batch jobs. The memory usage of the SQL server grows every time we run the batch and did not release it (the memory).

    As a temporary solution, we have to stop and start the SQL server.

    To us, it looks like a memory leaks, however Microsoft technical does not agree with it.

    Do you have any hints or tips how to resolve it. Perhaps some option parameters that we have to check on or off ?

    Thank you.

    Regards,
    T Tan
    tantekh@manulife.com

  • Anonymous User
    8 years ago
    Nov 17, 2004

    I have stored procedure that I am running in SQL Server 2000 that uses the OpenQuery(). This works great when I run it from SQL Server, but I try to execute it in a Visual Basic app using ADO and I get the following message:
    [OLE/DB provider returned message: [Microsoft][ODBC driver for Oracle]Driver not capable].

    I was wondering if anyone knew of a solution as to why this happens. Many thanks in advance.

    Eddie

  • Anonymous User
    8 years ago
    Nov 04, 2004

    I am using IBMDADB2 as well and the four part query was returning the same responses for me. It was only when I used openquery() that I was able to get results.

  • Nalina
    8 years ago
    Mar 08, 2004

    I tried to create linked server to access in SQL sever using the steps outlined in this article. However, I keep getting the error 7339 message .
    I tried to look up explanation for this error on the net and tried to resolve it based on the suggestions listed in those articles but with no success.
    Also I tried to set up a linked server to dataware house its giving me err 7303, could not initialize the provider IBMDADB2, this comes if I use the provider IBM OLE DB Provider for DB2 servers, the DSN for datawarehouse has been set up using IBM DB2 ODBC DRIVER.
    If I use MS OLE DB FOR ODBC DRIVER for datawarehouse than I get err 7399.
    Can you please let me where I am going wrong. I have wasted a lot of time on this.
    Your help is much appreciated
    Nalina

You must log on before posting a comment.

Are you a new visitor? Register Here