StarQuest Technical Documents

How to Use a Microsoft SQL Server Linked Server to Access DB2 with StarSQL

Last Update: 26 June 2008
Product: StarSQL
Version: 5.x
Article ID: SQV00SQ004

Abstract

SQL Server linked servers can be used to access DB2 through a StarSQL ODBC data source. This document demonstrates how to add and use a linked server. These instructions apply to SQL Server 7, SQL Server 2000, SQL Server 2005, and SQL Server 2008.

Solution

Perform the following steps to configure, test, and use a linked server.

Configure a Linked Server

  1. Launch the SQL Server 7/2000 Enterprise Manager or SQL Server 2005/2008 Management Studio.
  2. Under the Security folder (SQL Server 7/2000) or the Server Objects folder (SQL Server 2005/2008), right-click on Linked Servers and select New Linked Server.
  3. Enter in a name for the Linked Server and choose "Other data source". Do not use any spaces or special characters in the Linked Server name.
  4. Select "Microsoft OLE DB Provider for ODBC Drivers" as the Provider Name. On SQL Server 2005/2008, enter MSDASQL for the Product Name.
  5. In the "Data source" field, enter in the name of the ODBC system data source you wish to use. Note that you must enter in the Data Source name exactly as it is shown in the ODBC Data Source Administrator.
  6. For SQL Server 7/2000, click the "Security" tab and select "Be made with this security context". For SQL Server 2005/2008, select the Security page and select "Be made using this security context". Enter a valid remote user/login (user ID) and password for the remote host.
  7. Leave the remaining fields blank and click OK to save the linked server configuration.

Test the Linked Server

If an error occurs while testing the linked server connection, review the error and make changes to the linked server configuration as necessary.

SQL Server 7/2000:

In the Enterprise Manager, expand the new linked server and click on Tables to display a table list.  

SQL Server 2005:

In the Management Studio, click on the New Query button and execute a query using the samples below as a guideline.

SQL Server 2008:

In the Management Studio, expand the linked server node and then expand the Catalogs node. Click on the node for the linked server and expand the Tables to view a table list.

Sample Linked Server SQL Syntax

The following examples demonstrate how to use a linked servers to execute SELECT, INSERT, UPDATE, and DELETE statements.  Execute these statements in either the SQL Server 7/2000 Query Analyzer or the SQL Server 2005/2008 New Query field. These examples assume the following:

  • "STARSQLDSN" is a linked server that accesses DB2 through a StarSQL data source.
  • TITLES is a valid table located within a library (or with a schema name) called BOOKS, TITLE is a valid column in the TITLES table.

To perform a SELECT on the table:

SELECT * FROM STARSQLDSN.<catalog>.BOOKS.TITLES

OR

SELECT * FROM OPENQUERY (STARSQLDSN, 'SELECT * FROM BOOKS.TITLES')

To INSERT a row into the table:

INSERT INTO STARSQLDSN.<catalog>.BOOKS.TITLES (TITLE) VALUES ('The Grapes of Wrath')

OR

INSERT INTO OPENQUERY (STARSQLDSN, 'SELECT * FROM BOOKS.TITLES') (TITLE) VALUES ('The Grapes of Wrath')

To UPDATE a row in the table:

UPDATE STARSQLDSN.<catalog>.BOOKS.TITLES SET TITLE = 'Of Mice and Men' WHERE TITLE = 'The Grapes of Wrath'

OR

UPDATE OPENQUERY (STARSQLDSN, 'SELECT * FROM BOOKS.TITLES') SET TITLE = 'Of Mice and Men' WHERE TITLE = 'The Grapes of Wrath'

To DELETE a row in the table:

DELETE STARSQLDSN.<catalog>.BOOKS.TITLES WHERE TITLE = 'The Grapes of Wrath'

OR

DELETE OPENQUERY (STARSQLDSN, 'SELECT * FROM BOOKS.TITLES') WHERE TITLE = 'The Grapes of Wrath'

NOTE: To perform DELETE or UPDATE operations, the target table must have a unique index. For more information, please refer to the section titled UPDATE and DELETE Requirements for OLE DB Providers in the SQL Server Books Online.


DISCLAIMER

The information in technical documents comes without any warranty. The author(s) or distributor(s) will not accept responsibility for any damage incurred directly or indirectly through use of the information contained in these documents. The information in technical documents may be gathered from various sources, including IBM, Microsoft, and other organizations.