Creating a Linked Server to Access Excel Files in SQL Server

In the world of data management, there’s often a need to import data from various sources, and Microsoft SQL Server provides an excellent solution for this by allowing you to create a linked server to access data from external sources, including Excel files. In this blog post, we will explore how to set up a linked server to access Excel files in SQL Server, and we’ll provide examples, code snippets, and snapshots to guide you through the process.

What is a Linked Server?

A linked server in SQL Server allows you to access data from an external data source, as if that data source were a part of your SQL Server instance. It enables you to run distributed queries, update data, and even join tables between your SQL Server database and other data sources.

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.

Step 1: Enable the ‘Ad Hoc Distributed Queries’ Option

First, we need to enable the ‘Ad Hoc Distributed Queries’ option, which is required to set up a linked server. Run the following SQL command to enable it:

Step 2: Create a Linked Server

Now, let’s create a linked server that connects to our Excel file. We’ll use the SQL Server Management Studio (SSMS) to do this:

1. Open SSMS and connect to your SQL Server instance.

2. In Object Explorer, navigate to ‘Server Objects’ and right-click on ‘Linked Servers.’ Then, choose ‘New Linked Server…’

How to create Linked server from SQL Server to Azure SQL ...

3. In the ‘New Linked Server’ dialog, you’ll need to configure several settings:

   – General: Enter a name for your linked server in the ‘Linked server’ field.

   – Server Type: Choose ‘Other data source.’

   – Provider: Select ‘Microsoft Office 12.0 Access Database Engine OLE DB Provider’ (or the appropriate version).

   – Product Name: Enter ‘Excel’ or any name you prefer.

   – Data Source: Provide the full path to your Excel file, such as `C:\Path\To\Your\File.xlsx`.

   – Provider String: Enter `Excel 12.0` (adjust the version according to your Excel version).

A screenshot of a computer

Description automatically generated

4. After configuring these settings, navigate to the ‘Security’ tab and choose the security options that match your environment. You can use the current login’s security context or specify a remote login and password.

5. Click ‘OK’ to create the linked server.

Step 3: Accessing Excel Data

You can now use T-SQL queries to access data from your Excel file as if it were a regular table. Here’s an example of retrieving data from the Excel file:

A screenshot of a computer

Description automatically generated

You can perform various SQL operations like filtering, joining, and aggregating data from the Excel sheet.

Step 4: Importing Excel Data into SQL Server Tables

You can import data from Excel into SQL Server tables using `SELECT INTO` statements. Here’s an example of how to create a new table and populate it with data from Excel:

A screenshot of a computer

Description automatically generated

Now you have successfully created a linked server to access Excel files in SQL Server, retrieved data from Excel, and imported it into a SQL Server table.

Conclusion:

Using linked servers to access Excel files in SQL Server can be a powerful tool for integrating data from various sources into your database. It enables you to perform SQL operations on Excel data seamlessly. By following the steps outlined in this blog post, you can set up your linked server, query your Excel data, and even import it into your SQL Server tables.

Remember to handle security and permissions carefully and ensure that your Excel file paths are correctly specified. With this knowledge, you can efficiently work with Excel data within your SQL Server environment.

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.