To read an Excel file using SQL, you must use a bridge because SQL is a language for querying databases, while Excel is a file-based spreadsheet. The most common way to achieve this is by using the OPENROWSET function in SQL Server, importing the data into a permanent table via an Import Wizard, or using a linked server. These methods allow you to treat an Excel sheet as if it were a standard SQL table, enabling you to run SELECT queries, filters, and joins against your spreadsheet data without manually typing every row into a database.

What You'll Need

  • A SQL database environment (such as Microsoft SQL Server).
  • The Microsoft Access Database Engine redistributable installed on the server (required for the SQL engine to understand the .xlsx format).
  • An Excel file (.xlsx or .xls) saved in a location accessible by the SQL Server service account.
  • Administrative permissions (sysadmin role) to enable advanced options like Ad Hoc Distributed Queries.
  • The exact file path to your spreadsheet (e.g., C:\Imports\Data.xlsx).

Step-by-Step

Depending on whether you want a one-time import or a live query, choose one of the following three methods. Note that menu labels may differ slightly based on your version of SQL Server Management Studio (SSMS) or your specific region.

Method 1: Using OPENROWSET for a Quick Query

This method is best when you need to read data once without importing it into a permanent table. It reads the file directly from the disk.

  1. Enable Ad Hoc Distributed Queries: By default, SQL Server blocks external file access for security. Open a new query window and execute the following commands to enable this feature:
    EXEC sp_configure 'show advanced options', 1; RECONFIGURE;
    EXEC sp_configure 'Ad Hoc Distributed Queries', 1; RECONFIGURE;
  2. Verify File Permissions: Ensure the Windows account that runs the SQL Server service has "Read" permissions for the folder where the Excel file is stored. If the service account cannot see the folder, the query will fail with an "Access Denied" error.
  3. Write the SELECT Query: Use the OPENROWSET function. You must specify the provider (Microsoft.ACE.OLEDB.12.0), the data source (the path), and the mode. The query should look like this:
    SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0; Database=C:\Path\To\YourFile.xlsx', 'SELECT * FROM [Sheet1$]');
  4. Execute and Review: Run the query. The result grid will display the contents of "Sheet1" as a virtual table.

Method 2: Using the SQL Server Import and Export Wizard

This is the most reliable method for beginners and those who want to move data permanently into a database table for faster performance.

  1. Launch the Wizard: Right-click on your target database in the Object Explorer. Navigate to Tasks > Import Data.
  2. Choose a Data Source: In the "Choose a Data Source" dropdown, select "Microsoft Excel". Click the Browse button to locate your .xlsx file. Ensure the "First row has column names" checkbox is selected if your spreadsheet has headers.
  3. Choose a Destination: Select "SQL Server Native Client" or the "Microsoft OLE DB Driver for SQL Server". Verify the server name and the database destination are correct.
  4. Select Tables and Views: Choose "Copy data from one or more tables or views". Select the specific sheet you want to import from the list. You can click "Edit Mappings" to change the data types (e.g., changing a float to a decimal) or rename the destination table.
  5. Run the Package: Click Next through the remaining prompts and then click Finish. SQL Server will create a new table and populate it with the Excel data.

Method 3: Creating a Linked Server

Use this method if you have a spreadsheet that is updated frequently and you want to query it like a permanent table without re-importing it every time.

  1. Configure the Provider: In Object Explorer, navigate to Server Objects > Linked Servers > Providers. Right-click "Microsoft.ACE.OLEDB.12.0" and select Properties. Enable the "Allow inprocess" option.
  2. Create the Link: Use the sp_addlinkedserver stored procedure. You will define the provider and the data source (the path to the file).
    EXEC sp_addlinkedserver 'ExcelSource', 'Excel 12.0', 'Microsoft.ACE.OLEDB.12.0', 'C:\Path\To\YourFile.xlsx';
  3. Query the Linked Server: You can now query the file using a four-part naming convention: SELECT * FROM ExcelSource...[Sheet1$].

Common Mistakes

  • Incorrect Sheet Naming: A common error is forgetting the dollar sign ($) at the end of the sheet name. In SQL, Excel sheets must be referenced as [Sheet1$], not just [Sheet1].
  • File Locking: If the Excel file is currently open in Microsoft Excel, SQL Server may be unable to access it, resulting in a "file in use" or "cannot open" error. Always close the file before running the query.
  • Data Type Mismatches: Excel is loosely typed, meaning one column can contain both numbers and text. SQL is strictly typed. If a column has mixed data, SQL may import the entire column as NVARCHAR or return NULLs for cells that don't match the guessed data type.
  • Incorrect Provider Version: Using the 64-bit version of SQL Server with the 32-bit version of the Access Database Engine (or vice versa) will cause the provider to fail to load. The architectures must match.

Tips

  • Clean Your Data First: Before importing, remove empty rows at the bottom of your Excel sheet and ensure there are no merged cells. Merged cells often cause the import wizard to create unnecessary columns or shift data incorrectly.
  • Use CSV for Simplicity: If you don't need complex Excel formatting, save the file as a .csv (Comma Separated Values). SQL Server handles CSVs much more efficiently via the BULK INSERT command, which doesn't require the Access Database Engine.
  • Stage Your Data: Instead of querying the Excel file directly in a production query, import it into a "staging table" (a temporary table). Once the data is in SQL, you can clean it using SQL commands and then move it to your final production table.
  • Check Permissions: If you are using a network drive, ensure the SQL Server service account has network access. Local paths (C:\) are generally easier to configure for initial testing.

Troubleshooting

If the process does not go to plan, follow these steps in order of complexity:

  1. Check if the file is open: Close Excel completely and try again. This is the cause of most "Cannot open" errors.
  2. Verify the Provider: If you see an error stating "Provider not registered," you likely lack the Microsoft Access Database Engine. Download and install the version that matches your SQL Server's bit-architecture (x64 or x86).
  3. Test Path Accessibility: Try moving the file to a simple path like C:\Temp\data.xlsx. Long file paths or paths with spaces can sometimes cause syntax errors in connection strings.
  4. Check the Service Account: If you get an "Access Denied" error despite being an admin on the PC, remember that the SQL Server service (not you) is the one reading the file. Go to Services.msc, find SQL Server, and check the "Log On As" account. Give that specific account permissions to the folder.
  5. Restart the SQL Service: After installing the Access Database Engine or changing provider properties (like "Allow inprocess"), a restart of the SQL Server service is often required for changes to take effect.

Frequently Asked Questions

Can I read an Excel file without installing the Access Database Engine?

Not directly using the OLEDB provider. However, you can save the Excel file as a CSV and use the BULK INSERT or OPENROWSET(BULK...) command, which does not require external drivers.

Why are some of my numbers appearing as NULL after import?

This usually happens because of the "Type Guessing" mechanism. SQL Server looks at the first few rows to determine the data type. If the first 8 rows are numbers but the 9th is text, SQL may mark the text as NULL. To fix this, ensure the Excel column is formatted consistently or use the Import Wizard to manually set the column to NVARCHAR.

Is it possible to write data back to Excel using SQL?

Yes, using INSERT INTO OPENROWSET, but it is highly unstable and not recommended for production. It is much safer to export data from SQL to a CSV file and open that in Excel.

Which is faster: OPENROWSET or the Import Wizard?

The Import Wizard is faster for subsequent queries because it moves the data into the SQL Server's own storage engine. OPENROWSET must parse the Excel file every single time the query is run, which is significantly slower for large datasets.

Do I need to install Excel on the server to read the files?

No, you do not need the full Microsoft Excel application. You only need the Access Database Engine (the driver), which provides the necessary libraries for SQL Server to interpret the .xlsx file structure.