Subscribe to Windows IT Pro
June 16, 2000 10:10 AM

Linked Servers

SQL Server Pro
InstantDoc ID #8992
Rating: (3)
Downloads
8992.zip

The second way to query a linked server is to use the OPENQUERY command. This command is perfect for heterogeneous databases because it executes the requested query on the remote system, not on the SQL Server system that hosts the linked server. I've had many queries that wouldn't work with the four-part identifier but work fine with the OPENQUERY command. When you use the OPENQUERY command, you instruct SQL Server to select all records that the query specifies between the quotation marks:

SELECT * FROM OPENQUERY(LINKEDSERVER, "SELECT * FROM northwind..Categories where CategoryName Like 'Sea%'")

The first required piece of syntax is the linked server name, followed by the query. Notice that you need to use single quotes around the conditional piece of the query.

The third way to query a remote provider is to use the OPENROWSET command, which doesn't require that you have a linked server set up beforehand. This command uses linked server technology but creates the link at runtime, letting you dynamically set up the server you want to connect to. The OPENROWSET command operates the same as the OPENQUERY command: It executes all queries on the remote server. The example query in Listing 1 uses the OPENROWSET command. Listing 1's example code also shows that you can use the OPENROWSET (or OPENQUERY) command to perform joins.

Although OPENROWSET gives you additional flexibility and power to dynamically create connections, I don't recommend using this command unless you can absolutely justify a need. Having a centralized location where you can configure your linked server is much more convenient than having to recompile your code every time you make a connection change.

Gathering Meta Data
The stored procedure I use most for gathering linked server data is sp_linkedservers. This stored procedure tells you how many linked servers are configured and gives you information about them. The procedure is handy if you have obscure names for your linked servers because remembering such names can be difficult. If you haven't established a linked server, you'll see only one result: the local server's information.

You can also request a list of databases that are on your linked servers by using the sp_catalogs stored procedure. This stored procedure uses the @server_name variable, which is the name of the linked server you're querying:

sp_catalogs @server_name = 'LINKEDSERVER'

You can request a list of tables in a database by using the sp_tables_ex stored procedure:

EXEC sp_tables_ex @table_server = 'LINKEDSERVER', @table_catalog='northwind', @table_schema='dbo', @table_name='Suppliers'

Using some of the nonrequired parameters, such as @table_catalog and @table_name, is important when you want a list of only a subset of the database's tables. If you don't use the @table_name parameter, for example, the stored procedure will return every table in the database. If you don't use the @table_catalog parameter, the stored procedure will return the default database's tables only.

You can use several other stored procedures to obtain meta data about your linked server's tables and indexes. For a complete list of other stored procedures, see SQL Server Books Online (BOL).

Many companies can't or don't plan to upgrade their non-SQL Server infrastructures to SQL Server either because of the cost or because of the complexity. In these cases, linked servers let you build applications that take advantage of SQL Server's features but still access your legacy data. Linked servers also let you partition your data geographically or logically, dividing the load among the various servers.

Related Content:

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

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.