Using a SQL Server Linked Server to Query Excel Files
In my work place, we have Data Analyst/Data Integration people who constantly import Excel files into SQL for queries. Instead of importing the data to SQL Server, is there another way to query an Excel file using SQL Server tools?
There are several methods of doing this and one of them is to use a linked server which we will cover in this tip..
First create an Excel file named MyTable.xls. It is just an empty Excel file. (Note: you can download the Excel files and code here)
Next, create a new Linked Server pointing to Mytable.xls that was just created.
From Management Studio--> Object Explorer-->ServerObjects-->Right click Linked Servers to open a new Linked Server window.
Name the Linked Server "QUERYEXCEL" as shown below. For server type, choose "Other Data Sources". In the data source field, enter the full path of the MyTable.xls file you just created. Enter other fields as seen in the figure below.
To do a quick test drive, I have attached MyTable.xls file and also Employee.xls and sales.xls which you can download here.
To make it little interesting, Sales.xls has two sheets, with sales header data in Sheet1 and sales detail in sheet2. Also, remember to save these excel files to the folder on the SQL Server where the new linked server was just created, not on your desktop :)
You would see this
Lets query the Employee.xls
You would see this:
Lets query the Sales.xls, Sales Header is on Sheet1
Lets query the Sales.xls, Sales Detail is on Sheet2
Join Header and Detail from Sheet1 and Sheet2
This is very simple, but at the same time pretty helpful to quickly query and join Excel files with other tables thus avoid creating unwanted tables in the database.
- Renaming the files each time may not be the easiest approach, but hopefully you got the idea of how this could be done.
- Take a look at these other tips that are related to this topic:
About the author
View all my tips