Dave Mendlen
Visual Studio
November 1998
Summary: Examines the use of Microsoft® SQL Server™ 7.0's new linked servers to create a heterogeneous query that spans several data sources like Oracle, Access, and SQL Server 7.0. (8 printed pages) Covers:
With SQL Server 7.0's new linked servers you can create distributed queries that access data from Oracle, DB2, Sybase and any other data provider with an OLE DB driver. Now you have the ability to issue distributed queries, updates, commands, and transactions against all the data sources across the enterprise and return the results to a single result set. You can create a Web page that joins all the Customer information in DB2 with all the Order information in Oracle, joined to all the payment information from Informix.
Figure 1. Distributed query using linked servers
To run the query shown in Figure 1 without SQL Server 7.0's linked servers, you would have to write an Oracle PL/SQL command, and then a Microsoft Access JET/SQL command, and then a SQL Server Transact-SQL command. With linked servers, you can issue a single Transact-SQL command that will request data from all the data sources. And now you don't even have to write the SQL: You can do it all using the graphical query builder tools provided in Visual Studio.
Figure 2. Linked server configuration
Figure 2 shows the basics of how a linked server configuration functions.
Linked servers are typically used to handle distributed queries. When a client application executes a distributed query through a linked server, SQL Server breaks down the command and sends rowset requests to OLE DB. OLE DB then queries the provider for the rowset information. The provider, in turn, knows the specifics of calling the data source, opening the file, and returning the requested information. The rowset data is returned through the provider and OLE DB to SQL Server, where it is reconstructed and returned to the client application as a result set and set of output parameters (if applicable).
This tutorial will show you how to connect to both Oracle and Access through SQL Server 7.0's new linked servers. This tutorial will work with either Oracle or Access or both connected to SQL 7.0. So if you don't have Oracle connected but want to see how to establish a linked server, jump down to the Access section and skip the Oracle setup.
The first thing you need to do is to create a new linked server. The first linked server that we'll create will connect to Oracle. Because this technology utilizes SQL*NET, you can go against Oracle on any platform where Oracle runs.
Log in to SQL Server as an administrator. The user ID is probably "sa," with no password. Make sure you are in the master database context.
Figure 3. SQL Server Query Analyzer
This is a static, named connection to Oracle that you'll be using later. To create a new linked server, you can use the system stored procedure, sp_addlinkedserver.
You need to replace the following variable:
EXEC sp_addlinkedserver 'SQLNET', 'Oracle', 'MSDAORA', 'SQLNET'
Run the query.
Now you need to create a SQL Server user login with permissions to log into your Oracle database. The sp_addlinkedsrvlogin system stored procedure creates the login and connects it to the new linked server.
You need to replace the following variables:
EXEC sp_addlinkedsrvlogin 'SQLNET', 'FALSE', NULL, 'system', 'manager'"
Run the query.
One of the easiest ways to manipulate these connections to your remote data in Visual Studio is through Views. In the Query Analyzer, change your database context to Pubs. The view that we'll be creating is based on the Scott/Tiger demo database that comes with Oracle.
You need to replace the following variable:
Create View OracleSales as SELECT * FROM SQLNET..DEMO.SALES_ORDER
Run the query.
The second linked server that we'll create will connect to Access.
Log in to SQL Server as an administrator. The user ID is probably "sa," with no password. Make sure you are in the master database context.
This is a static, named connection to Access that you'll be using later. To create a new linked server, you can use the system stored procedure, sp_addlinkedserver. The database we're using works well in joining to both SQL Server 7.0 and Oracle.
You need to replace the following variable:
EXEC sp_addlinkedserver 'Foodmart', '','Microsoft.Jet.OLEDB.4.0', '
LinkedServer'
Run the query.
Now you need to create a SQL Server user login with permissions to log into your Access database. The sp_addlinkedsrvlogin system stored procedure creates the login and connects it to the new linked server. Unless you've set up security to this database, you can use the default Admin user ID for Access with no password.
You don't need to replace any variables.
EXEC sp_addlinkedsrvlogin 'Foodmart', false, NULL, 'Admin', NULL
Run the query.
One of the easiest ways to manipulate these connections to your remote data in Visual Studio is through Views. In the Query Analyzer, change your database context to Pubs. The view that we'll be creating is based on the FoodMart database that comes with DSS.
You don't need to replace any variables.
Create View AccessSales as SELECT customer_id AS Order_ID, product_id,
promotion_id, store_cost, store_id, store_sales, time_id, unit_sales
FROM Foodmart.. .sales_fact_1998 WHERE (customer_id < 650) AND
(customer_id > 500)
Finally, we'll create a SQL Server 7.0 view to be consistent with the rest of the views that we've created. We also need to insert some data that we can join on in the SQL Server database.
Log in to SQL Server as an administrator. The user ID is probably "sa," with no password. Make sure you are in the pubs database context.
We'll be using the SQL Server 7.0 Pubs database, which has a Sales table. This example view can be joined to the Oracle and Access views that we created earlier.
You don't need to replace any variables.
Create View SQL7Sales as SELECT stor_id, cast(ord_num AS int) AS
order_ID, ord_date, qty, payterms, title_id FROM sales WHERE
isnumeric(ord_num) = 1
Run the query.
Now we need to add some rows of data to the SQL Server 7.0 Sales table so that all three data sources have rows that are in common.
You don't need to replace any variables.
INSERT INTO sales (stor_id, ord_num, ord_date, qty, payterms, title_id)
VALUES ('6380', '511', '6/3/98', 82, 'Net 30', 'BU1032')
INSERT INTO sales (stor_id, ord_num, ord_date, qty, payterms, title_id)
VALUES ('6380', '610', '6/1/98', 50, 'Net 30', 'BU1032')
INSERT INTO sales (stor_id, ord_num, ord_date, qty, payterms, title_id)
VALUES ('6380', '520', '7/3/98', 12, 'Net 30', 'BU1032')
INSERT INTO sales (stor_id, ord_num, ord_date, qty, payterms, title_id)
VALUES ('6380', '599', '1/25/98', 34, 'Net 30', 'BU1032')
Run the query.
Let's create a new query that joins data from all three data sources.
Figure 4. SQL Server Login
Figure 5. Building the query
Now, simply choose the columns that you wish to use for the query by clicking next to the columns in all three tables.
If this is the first time that you have run the query, it might take a few seconds to establish all the connections to the databases. Rerun the query to see the query run immediately.
Manually seaming together data from multiple data sources is incredibly difficult. With linked servers, you can issue a single Transact-SQL command that will request data from all the different data sources that you have. And now you don't even have to write the SQL: You can do it all using the graphical query builder tools provided in Visual Studio 6.0.