OpenRowSource and OpenRowSet in SQL Server 2000 | Database Journal

OpenRowSource and OpenRowSet in SQL Server 2000

Apr 7, 2004
2 minute read

SQL Server Data Base Administrators are often exposed to
situations in running ad-hoc queries using external data sources such as MS-Access
database or Excel sheets or CSV files or text files. For such frequent requests,
Database Administrators usually use “linked servers” covered in my previous
article, Linking
SQL Server to Heterogeneous Systems
.

However, for in-frequent ad-hoc requests, Database
Administrators usually use openrowsource or openrowset, or they import the
external data source to SQL server and query tables. The main intent of this
article is to demonstrate how to use OpenRowSource and OpenRowset.

OpenRowset helps in accessing remote data from an OLE DB
data source. Openrowsource provides ad hoc connection information as part of a
four-part object name without using a linked server name.

How to query an Excel sheet using OpenDataSource

Let’s
create an Excel sheet c:\external\MyExcel.xls with Book1 as the workbook and
create some sample data. Open Query analyzer and execute the SQL statement
below.

Read Excel Sheet using OpenDataSource
SELECT *
FROM OpenDataSource( ‘Microsoft.Jet.OLEDB.4.0’,
  ‘Data Source=”c:\External\MyExcel.xls”;
    User ID=Admin;Password=;Extended properties=Excel 8.0′)…Book1$

The result will look like
the table below:

SmithWilliam1000275
ScottTiger2000275
JohnJumangi2500345
SamRooban3524600
PeterNorton1234320
KathyLee83001200

How to query an Excel
sheet using OpenRowSet

Let’s create an Excel sheet c:\external\MyExcel.xls with
Book1 as the workbook and create some sample data. Open Query analyzer and
execute the SQL statement below.

Read Excel Sheet using OpenRowSet
select * FROM OPENROWSET(‘Microsoft.Jet.OLEDB.4.0’,
‘Excel 8.0;DATABASE=c:\External\MyExcel.xls’, ‘Select * from [Book1$]’)

The result will look like the
table below:

SmithWilliam1000275
ScottTiger2000275
JohnJumangi2500345
SamRooban3524600
PeterNorton1234320
KathyLee83001200
Advertisement

How to query a remote SQL
Server table using OpenDataSource

Let’s create a table named CM_Sales.dbo.users
on a remote server ETL. Execute the SQL statement below.

Read SQL Server table using OpenDataSource
SELECT   *
FROM      OPENDATASOURCE(
         ‘SQLOLEDB’,
         ‘Data Source=ETL;User ID=Weblogin;Password=Web’
         ).CM_Sales.dbo.users

The results will look like the
table below:

22MASTER9A7B3CC347CD336A
1122JTOWSLEE9A7B3CC347CD336A
1222RHOWENULL
1572BMOELLERNULL
1322CFINETTINULL
1422LTHOMASNULL

How to query a remote SQL
Server table using OpenRowset

Let’s create a table named CM_Sales.dbo.users
on a remote server ETL. Execute the SQL statement below.

Read SQL Server table using OPENROWSET
SELECT *
FROM OPENROWSET(‘MSDASQL’,
   ‘DRIVER={SQL Server};SERVER=ETL;UID=Weblogin;PWD=web’,
   CM_Sales.dbo.Users)

The results will look like the
table below:

22MASTER9A7B3CC347CD336A
1122JTOWSLEE9A7B3CC347CD336A
1222RHOWENULL
1572BMOELLERNULL
1322CFINETTINULL
1422LTHOMASNULL
Database Journal Logo

DatabaseJournal.com publishes relevant, up-to-date and pragmatic articles on the use of database hardware and management tools and serves as a forum for professional knowledge about proprietary, open source and cloud-based databases--foundational technology for all IT systems. We publish insightful articles about new products, best practices and trends; readers help each other out on various database questions and problems. Database management systems (DBMS) and database security processes are also key areas of focus at DatabaseJournal.com.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.