Article ID: 306397 - Last Review: November 2, 2007 - Revision: 6.4 How to use Excel with SQL Server linked servers and distributed queriesThis article was previously published under Q306397 On This PageSUMMARYMicrosoft SQL Server supports connections to other OLE DB data
sources on a persistent or an ad hoc basis. The persistent connection is known
as a linked server; an ad hoc connection that is made for the sake of a single
query is known as a distributed query. Microsoft Excel workbooks are one type of OLE DB data source that you can query through SQL Server in this manner. This article describes the syntax that is necessary to configure an Excel data source as a linked server, as well as the syntax that is necessary to use a distributed query that queries an Excel data source. MORE INFORMATIONQuerying an Excel data source on a linked serverYou can use SQL Server Management Studio or Enterprise Manager, a system stored procedure, SQL-DMO (Distributed Management Objects), or SMO (SQL Server Management Objects) to configure an Excel data source as a SQL Server linked server. (SMO are only available for Microsoft SQL Server 2005.) In all of these cases, you must always set the following four properties:
Note If you are using SQL Server 2005, you must specify a value that is not empty for the Product name property in SQL Server Management Studio or for the @srvproduct property in the stored procedure for an Excel data source. Using SQL Server Management Studio or Enterprise Manager to configure an Excel data source as a linked serverSQL Server Management Studio (SQL Server 2005)
Enterprise Manager (SQL Server 2000)
Using a stored procedure to configure an Excel data source as a linked serverYou can also use the system stored procedure sp_addlinkedserver to configure an Excel data source as a linked server:Using SQL-DMO to configure an Excel data source as a linked serverYou can use SQL Distributed Management Objects to configure an Excel data source as a linked server programmatically from Microsoft Visual Basic or another programming language. You must supply the same four arguments that are required in the Enterprise Manager and SQL Server Management Studio configuration.Using SMO to configure an Excel data source as a linked serverIn SQL Server 2005, you can use SQL Server Management Objects (SMO) to configure an Excel data source as a linked server programmatically. To do this, you can use Microsoft Visual Basic .NET or another programming language. You must supply the arguments that are required in the SQL Server Management Studio configuration. The SMO object model extends and supersedes the Distributed Management Objects (SQL-DMO) object model. Because SMO is compatible with SQL Server version 7.0, SQL Server 2000, and SQL Server 2005, you can also use SMO for configuration of SQL Server 2000.Querying an Excel data source on a linked serverAfter you configure an Excel data source as a linked server, you can easily query its data from Query Analyzer or another client application. For example, to retrieve the rows of data that are stored in Sheet1 of your Excel file, the following code uses the linked server that you configured by using SQL-DMO:You can also obtain a list of all the tables that are available on the Excel linked server by using the following query: Querying an Excel data source by using distributed queriesYou can use SQL Server distributed queries and the OPENDATASOURCE or OPENROWSET function to query infrequently accessed Excel data sources on an ad hoc basis.Note If you are using SQL Server 2005, make sure that you have enabled the Ad Hoc Distributed Queries option by using SQL Server Surface Area Configuration, as in the following example: Could not find installable ISAM. REFERENCES Because SQL Server linked servers and distributed queries
use the OLE DB Provider, the general guidelines and cautions about using ADO
with Excel apply here.
For more information, click the following article number to view the article in the Microsoft Knowledge Base:
257819
(http://support.microsoft.com/kb/257819/
)
How to use ADO with Excel data from Visual Basic or VBA
For more information about SQL Server Management Objects, visit the following Microsoft Developer Network (MSDN) Web site:http://msdn2.microsoft.com/en-us/library/ms162169(ide).aspx
(http://msdn2.microsoft.com/en-us/library/ms162169(ide).aspx)
For more information about how to enable the Ad Hoc Distributed Queries option, visit the following MSDN Web site:http://msdn2.microsoft.com/en-us/library/ms189978(ide).aspx
(http://msdn2.microsoft.com/en-us/library/ms189978(ide).aspx)
APPLIES TO
| Article Translations
|

Back to the top
