SQL Server - Querying an Excel Workbook From an SQL Query

Using a SQL Server SELECT Statement to Query an Excel Workbook

Occasionally you may find that some of the data you need to reference in a SQL Server query is located outside of the database in an Excel Workbook.

In this article we look at how you can query an Excel workbook as if it were a table in a SQL Server Database.

The SQL Server OPENROWSET function can be used to connect to a variety of data sources by means of a data provider: 

Excel - Creating a Table

Creating And Working With Tables in Excel

You may think that an Excel Workbook already provides a table of data to you, and in a sense it does, an area consisting of Rows and Columns, with column and row headings and values.

It is, however, possible to create formal Tables from ranges of cells on an Excel worksheet.

Why would we want to do this?

Well, here are a few reasons:

Excel - Creating drop Down Lists in Cells

Creating A Drop Down Lists For Users to Choose From In Excel

When you design an Excel workbook for users other than yourself to use you may find that it is helpful to provide drop down lists for some column values such as Status Codes, Categories, Response Codes, etc. This is surprisingly easy in Excel and requires no clever VBA code!

In the following example we can see a drop list that a user can choose from when populating a Country column value.

Using Excel VBA to Query a SQL Server Database

Although we can create External Data Sets in Excel that pull information from a SQL Server Database there are times when you want to check a value or lookup a value from an underlying SQL Server database directly from an Excel formula.

Using VBA we can do this!

We will look at an example that enables us to lookup a customer’s total revenue value from an Excel Formula.

 

Excel - Transpose Columns & Rows

Make Rows the Columns and Make Columns the Rows In an Excel Worksheet

Have you ever found that you have a worksheet with a table of data on it, but you would really like the rows and columns to be switched round? In other words you would like to transpose the columns and rows.

Excel - De-Duplicating Data

Stripping Out Duplicate Data From an Excel Worksheet

We often find that after converging data from a number of sources we have duplicate records in our worksheets. In this article I show you how to strip out those duplicate rows using the Excel “Remove Duplicates” option.

De-duplicating A Single Excel Column

In the following example we have duplicate company names in the data on our worksheet.

Pages