Accessing Excel Files Using OPENROWSET and OPENDATASOURCE in SQL Server

In the world of data management, integration, and extraction of data from external sources like Excel files are common tasks. SQL Server provides various methods to achieve this, and in this blog post, we’ll explore two of them: `OPENROWSET` and `OPENDATASOURCE`. We’ll provide examples, code snippets, and snapshots to guide you through the process.

Prerequisites:

Before we begin, make sure you have the following:

1. Microsoft SQL Server installed and running.

2. The Excel file you want to access.

Method 1: Using OPENROWSET

`OPENROWSET` is an SQL Server function that allows you to access remote data sources, including Excel files. You can use it to read data from an Excel file directly into SQL Server.

The OPENROWSET function can be referenced in the FROM clause of a query as if it were a table name. The OPENROWSET function can also be referenced as the target table of an INSERT, UPDATE, or DELETE statement, subject to the capabilities of the OLE DB provider. Although the query might return multiple result sets, OPENROWSET returns only the first one.

Step 1: Enable Ad Hoc Distributed Queries

Just like when working with linked servers, you need to enable the ‘Ad Hoc Distributed Queries’ option. Run the following SQL commands to enable it:

Step 2: Query Excel Data Using OPENROWSET

Now, you can use `OPENROWSET` to query Excel data. Here’s an example:

You can adjust the `SELECT` statement to retrieve specific data from your Excel sheet.

Method 2: Using OPENDATASOURCE

`OPENDATASOURCE` is another method for accessing external data sources in SQL Server. It’s very similar to `OPENROWSET` but provides a more structured way of specifying the data source. 

The OPENDATASOURCE function can be used in the same Transact-SQL syntax locations as a linked-server name. Therefore, OPENDATASOURCE can be used as the first part of a four-part name that refers to a table or view name in a SELECT, INSERT, UPDATE, or DELETE statement, or to a remote stored procedure in an EXECUTE statement. When executing remote stored procedures, OPENDATASOURCE should refer to another instance of SQL Server. OPENDATASOURCE does not accept variables for its arguments.

Step 1: Query Excel Data Using OPENDATASOURCE

Here’s an example of how to use `OPENDATASOURCE` to query Excel data:

In this example, replace `’C:\Path\To\Your\File.xlsx’` with the actual path to your Excel file. Also, provide your database name, schema name, and table name as needed.

Conclusion:

Both `OPENROWSET` and `OPENDATASOURCE` are powerful tools for accessing Excel files in SQL Server. They allow you to read data from Excel sheets directly into your database, making it easier to integrate external data sources with your SQL Server environment.

When using these methods, remember to specify the correct Excel file path and provide the necessary connection properties, such as the OLE DB provider and version. Additionally, ensure that you have the required permissions to access the Excel file.

With these techniques, you can efficiently work with Excel data within your SQL Server database, making data integration and analysis tasks more manageable and efficient.

Addend Analytics is a Microsoft Gold Partner based in Mumbai, India, and a branch office in the U.S.

Addend has successfully implemented 100+ Microsoft Power BI and Business Central projects for 100+ clients across sectors like Financial Services, Banking, Insurance, Retail, Sales, Manufacturing, Real estate, Logistics, and Healthcare in countries like the US, Europe, Switzerland, and Australia.

Get a free consultation now by emailing us or contacting us.