title | description | author | ms.author | ms.date | ms.service | ms.subservice | ms.topic | monikerRange |
---|---|---|---|---|---|---|---|---|
Import data from Excel to SQL Server or Azure SQL Database |
This article describes methods to import data from Excel to SQL Server or Azure SQL Database. Some use a single step, others require an intermediate text file. |
rwestMSFT |
randolphwest |
03/30/2023 |
sql |
data-movement |
conceptual |
=azuresqldb-current||>=sql-server-2016||>=sql-server-linux-2017||=azuresqldb-mi-current |
Import data from Excel to SQL Server or Azure SQL Database
[!INCLUDE SQL Server Azure SQL Database]
There are several ways to import data from Excel files to [!INCLUDE ssnoversion-md] or to Azure SQL Database. Some methods let you import data in a single step directly from Excel files; other methods require you to export your Excel data as text (CSV file) before you can import it.
This article summarizes the frequently used methods and provides links for more detailed information. A complete description of complex tools and services like SSIS or Azure Data Factory is beyond the scope of this article. To learn more about the solution that interests you, follow the provided links.
List of methods
There are several ways to import data from Excel. You may need to install SQL Server Management Studio (SSMS) to use some of these tools.
You can use the following tools to import data from Excel:
Export to text first ([!INCLUDE ssnoversion-md] and SQL Database) | Directly from Excel ([!INCLUDE ssnoversion-md] on-premises only) |
---|---|
Import Flat File Wizard | SQL Server Import and Export Wizard |
BULK INSERT statement | SQL Server Integration Services (SSIS) |
BCP | OPENROWSET function |
Copy Wizard (Azure Data Factory) | |
Azure Data Factory |
If you want to import multiple worksheets from an Excel workbook, you typically have to run any of these tools once for each sheet.
[!IMPORTANT]
To learn more, see limitations and known issues for loading data to or from Excel files.
Import and Export Wizard
Import data directly from Excel files by using the [!INCLUDE ssnoversion-md] Import and Export Wizard. You also can save the settings as a SQL Server Integration Services (SSIS) package that you can customize and reuse later.
-
In [!INCLUDEssManStudioFull], connect to an instance of the [!INCLUDEssNoVersion] [!INCLUDEssDE].
-
Expand Databases.
-
Right-click a database.
-
Select Tasks.
-
Choose to Import Data or Export Data:
:::image type=»content» source=»../../integration-services/import-export-data/media/start-wizard-ssms.jpg» alt-text=»Start wizard SSMS»:::
This launches the wizard:
:::image type=»content» source=»media/excel-connection.png» alt-text=»Connect to an Excel data source»:::
To learn more, review:
- Start the SQL Server Import and Export Wizard
- Get started with this simple example of the Import and Export Wizard
Integration Services (SSIS)
If you’re familiar with SQL Server Integration Services (SSIS) and don’t want to run the [!INCLUDE ssnoversion-md] Import and Export Wizard, create an SSIS package that uses the Excel Source and the [!INCLUDE ssnoversion-md] Destination in the data flow.
To learn more, review:
- Excel Source
- SQL Server Destination
To start learning how to build SSIS packages, see the tutorial How to Create an ETL Package.
:::image type=»content» source=»media/excel-to-sql-data-flow.png» alt-text=»Components in the data flow»:::
OPENROWSET and linked servers
[!IMPORTANT]
In Azure SQL Database, you cannot import directly from Excel. You must first export the data to a text (CSV) file.
[!NOTE]
The ACE provider (formerly the Jet provider) that connects to Excel data sources is intended for interactive client-side use. If you use the ACE provider on [!INCLUDE ssnoversion-md], especially in automated processes or processes running in parallel, you may see unexpected results.
Distributed queries
Import data directly into [!INCLUDE ssnoversion-md] from Excel files by using the Transact-SQL OPENROWSET
or OPENDATASOURCE
function. This usage is called a distributed query.
[!IMPORTANT]
In Azure SQL Database, you cannot import directly from Excel. You must first export the data to a text (CSV) file.
Before you can run a distributed query, you have to enable the ad hoc distributed queries
server configuration option, as shown in the following example. For more info, see ad hoc distributed queries Server Configuration Option.
sp_configure 'show advanced options', 1; RECONFIGURE; GO sp_configure 'ad hoc distributed queries', 1; RECONFIGURE; GO
The following code sample uses OPENROWSET
to import the data from the Excel Sheet1
worksheet into a new database table.
USE ImportFromExcel; GO SELECT * INTO Data_dq FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0; Database=C:TempData.xlsx', [Sheet1$]); GO
Here’s the same example with OPENDATASOURCE
.
USE ImportFromExcel; GO SELECT * INTO Data_dq FROM OPENDATASOURCE('Microsoft.ACE.OLEDB.12.0', 'Data Source=C:TempData.xlsx;Extended Properties=Excel 12.0')...[Sheet1$]; GO
To append the imported data to an existing table instead of creating a new table, use the INSERT INTO ... SELECT ... FROM ...
syntax instead of the SELECT ... INTO ... FROM ...
syntax used in the preceding examples.
To query the Excel data without importing it, just use the standard SELECT ... FROM ...
syntax.
For more info about distributed queries, see the following articles:
- Distributed Queries (Distributed queries are still supported in [!INCLUDE sssql19-md], but the documentation for this feature hasn’t been updated.)
- OPENROWSET
- OPENDATASOURCE
Linked servers
You can also configure a persistent connection from [!INCLUDE ssnoversion-md] to the Excel file as a linked server. The following example imports the data from the Data
worksheet on the existing Excel linked server EXCELLINK
into a new [!INCLUDE ssnoversion-md] database table named Data_ls
.
USE ImportFromExcel; GO SELECT * INTO Data_ls FROM EXCELLINK...[Data$]; GO
You can create a linked server from SQL Server Management Studio (SSMS), or by running the system stored procedure sp_addlinkedserver
, as shown in the following example.
DECLARE @RC INT; DECLARE @server NVARCHAR(128); DECLARE @srvproduct NVARCHAR(128); DECLARE @provider NVARCHAR(128); DECLARE @datasrc NVARCHAR(4000); DECLARE @location NVARCHAR(4000); DECLARE @provstr NVARCHAR(4000); DECLARE @catalog NVARCHAR(128); -- Set parameter values SET @server = 'EXCELLINK'; SET @srvproduct = 'Excel'; SET @provider = 'Microsoft.ACE.OLEDB.12.0'; SET @datasrc = 'C:TempData.xlsx'; SET @provstr = 'Excel 12.0'; EXEC @RC = [master].[dbo].[sp_addlinkedserver] @server, @srvproduct, @provider, @datasrc, @location, @provstr, @catalog;
For more info about linked servers, see the following articles:
- Create Linked Servers
- OPENQUERY
For more examples and info about both linked servers and distributed queries, see the following article:
- How to use Excel with SQL Server linked servers and distributed queries
Prerequisite — Save Excel data as text
To use the rest of the methods described on this page — the BULK INSERT statement, the BCP tool, or Azure Data Factory — first you have to export your Excel data to a text file.
In Excel, select File | Save As and then select Text (Tab-delimited) (*.txt) or CSV (Comma-delimited) (*.csv) as the destination file type.
If you want to export multiple worksheets from the workbook, select each sheet, and then repeat this procedure. The Save as command exports only the active sheet.
[!TIP]
For best results with data importing tools, save sheets that contain only the column headers and the rows of data. If the saved data contains page titles, blank lines, notes, and so forth, you may see unexpected results later when you import the data.
The Import Flat File Wizard
Import data saved as text files by stepping through the pages of the Import Flat File Wizard.
As described previously in the Prerequisite section, you have to export your Excel data as text before you can use the Import Flat File Wizard to import it.
For more info about the Import Flat File Wizard, see Import Flat File to SQL Wizard.
BULK INSERT command
BULK INSERT
is a Transact-SQL command that you can run from SQL Server Management Studio. The following example loads the data from the Data.csv
comma-delimited file into an existing database table.
As described previously in the Prerequisite section, you have to export your Excel data as text before you can use BULK INSERT to import it. BULK INSERT can’t read Excel files directly. With the BULK INSERT command, you can import a CSV file that is stored locally or in Azure Blob storage.
USE ImportFromExcel; GO BULK INSERT Data_bi FROM 'C:Tempdata.csv' WITH ( FIELDTERMINATOR = ',', ROWTERMINATOR = 'n' ); GO
For more info and examples for [!INCLUDE ssnoversion-md] and SQL Database, see the following articles:
- Import Bulk Data by Using BULK INSERT or OPENROWSET(BULK…)
- BULK INSERT
BCP tool
BCP is a program that you run from the command prompt. The following example loads the data from the Data.csv
comma-delimited file into the existing Data_bcp
database table.
As described previously in the Prerequisite section, you have to export your Excel data as text before you can use BCP to import it. BCP can’t read Excel files directly. Use to import into [!INCLUDE ssnoversion-md] or SQL Database from a test (CSV) file saved to local storage.
[!IMPORTANT]
For a text (CSV) file stored in Azure Blob storage, use BULK INSERT or OPENROWSET. For an examples, see Example.
bcp.exe ImportFromExcel..Data_bcp in "C:Tempdata.csv" -T -c -t ,
For more info about BCP, see the following articles:
- Import and Export Bulk Data by Using the bcp Utility
- bcp Utility
- Prepare Data for Bulk Export or Import
Copy Wizard (ADF)
Import data saved as text files by stepping through the pages of the Azure Data Factory (ADF) Copy Wizard.
As described previously in the Prerequisite section, you have to export your Excel data as text before you can use Azure Data Factory to import it. Data Factory can’t read Excel files directly.
For more info about the Copy Wizard, see the following articles:
- Data Factory Copy Wizard
- Tutorial: Create a pipeline with Copy Activity using Data Factory Copy Wizard.
Azure Data Factory
If you’re familiar with Azure Data Factory and don’t want to run the Copy Wizard, create a pipeline with a Copy activity that copies from the text file to [!INCLUDE ssnoversion-md] or to Azure SQL Database.
As described previously in the Prerequisite section, you have to export your Excel data as text before you can use Azure Data Factory to import it. Data Factory can’t read Excel files directly.
For more info about using these Data Factory sources and sinks, see the following articles:
- File system
- SQL Server
- Azure SQL Database
To start learning how to copy data with Azure data factory, see the following articles:
- Move data by using Copy Activity
- Tutorial: Create a pipeline with Copy Activity using Azure portal
Common errors
Microsoft.ACE.OLEDB.12.0″ hasn’t been registered
This error occurs because the OLEDB provider isn’t installed. Install it from Microsoft Access Database Engine 2010 Redistributable. Be sure to install the 64-bit version if Windows and [!INCLUDE ssnoversion-md] are both 64-bit.
The full error is:
Msg 7403, Level 16, State 1, Line 3
The OLE DB provider "Microsoft.ACE.OLEDB.12.0" has not been registered.
Cannot create an instance of OLE DB provider «Microsoft.ACE.OLEDB.12.0» for linked server «(null)»
This indicates that the Microsoft OLEDB hasn’t been configured properly. Run the following Transact-SQL code to resolve this:
EXEC sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'AllowInProcess', 1; EXEC sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'DynamicParameters', 1;
The full error is:
Msg 7302, Level 16, State 1, Line 3
Cannot create an instance of OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)".
The 32-bit OLE DB provider «Microsoft.ACE.OLEDB.12.0» cannot be loaded in-process on a 64-bit SQL Server
This occurs when a 32-bit version of the OLD DB provider is installed with a 64-bit [!INCLUDE ssnoversion-md]. To resolve this issue, uninstall the 32-bit version and install the 64-bit version of the OLE DB provider instead.
The full error is:
Msg 7438, Level 16, State 1, Line 3
The 32-bit OLE DB provider "Microsoft.ACE.OLEDB.12.0" cannot be loaded in-process on a 64-bit SQL Server.
The OLE DB provider «Microsoft.ACE.OLEDB.12.0» for linked server «(null)» reported an error.
Cannot initialize the data source object of OLE DB provider «Microsoft.ACE.OLEDB.12.0» for linked server «(null)»
Both of these errors typically indicate a permissions issue between the [!INCLUDE ssnoversion-md] process and the file. Ensure that the account that is running the [!INCLUDE ssnoversion-md] service has full access permission to the file. We recommend against trying to import files from the desktop.
The full errors are:
Msg 7399, Level 16, State 1, Line 3
The OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)" reported an error. The provider did not give any information about the error.
Msg 7303, Level 16, State 1, Line 3
Cannot initialize the data source object of OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)".
Next steps
- Get started with this simple example of the Import and Export Wizard
- Import data from Excel or export data to Excel with SQL Server Integration Services (SSIS)
- bcp Utility
- Move data by using Copy Activity
В Microsoft SQL Server существует возможность в SQL запросах, на языке T-SQL, запрашивать данные из внешних источников, сегодня мы рассмотрим несколько способов обращения к файлу Excel из запроса с целью получения данных.
В языке T-SQL это можно реализовать с помощью так называемых «Распределенных запросов», которые предполагают использование следующих конструкций:
- OPENDATASOURCE
- OPENROWSET
- OPENQUERY
Сейчас мы рассмотрим все эти конструкции, и напишем несколько примеров.
Примечание! Загрузить данные из Excel можно и без использования языка T-SQL, в Microsoft SQL Server разработаны специальные службы SSIS (SQL Server Integration Services), в частности реализован инструмент по загрузке данных из разных источников «Импорт и экспорт данных», который позволяет загружать данные, уже не прибегая к использованию языка T-SQL.
Содержание
- Исходные данные и требования
- Функция OPENDATASOURCE в T-SQL
- Функция OPENROWSET в T-SQL
- Функция OPENQUERY в T-SQL
Исходные данные и требования
В качестве примера давайте представим, что нам нужно обратиться к файлу «TestExcel.xls», который расположен на диске D. Сами данные расположены на листе со стандартным названием «Лист1», они имеют следующую структуру.
ProductId | CategoryId | ProductName | Price |
1 | 1 | Клавиатура | 100 |
2 | 1 | Мышь | 50 |
3 | 2 | Телефон | 300 |
Все примеры будут выполнены в Microsoft SQL Server 2016 Express.
Для того чтобы начать пользоваться распределенными запросами, нам сначала нужно включить данную возможность на SQL сервере, так как по умолчанию она выключена.
Включается возможность использования распределённых запросов с помощью системной хранимой процедуры sp_configure, которая отвечает за системные параметры сервера. Нам необходимо параметру Ad Hoc Distributed Queries присвоить значение 1, для этого выполните следующую SQL инструкцию.
sp_configure 'show advanced options', 1; RECONFIGURE; GO sp_configure 'Ad Hoc Distributed Queries', 1; RECONFIGURE; GO
Параметром show advanced options мы отображаем дополнительные параметры системной хранимой процедуры sp_configure, а командой RECONFIGURE мы применяем новые параметры.
Функция OPENDATASOURCE в T-SQL
OPENDATASOURCE – функция возвращает ссылку на источник данных, который может использоваться как часть четырехсоставного имени объекта.
Данная функция принимает два параметра, первый (provider_name) — это имя поставщика OLE DB, второй (init_string) – строка подключения.
Пример.
SELECT * FROM OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0', 'Data Source=D:TestExcel.xls; Extended Properties=Excel 8.0')...[Лист1$];
Как видите, в функцию мы передали имя поставщика и строку подключения, а потом обратились к источнику, используя четырехсоставное имя объекта, т.е. к таблице на листе 1.
Функция OPENROWSET в T-SQL
OPENROWSET – функция подключается к источнику данных и выполняет необходимый запрос.
У данной функции уже много параметров, с помощью которых мы указываем все необходимые сведения о подключении, которые требуются для доступа к источнику данных OLE DB.
Для того чтобы обратиться к Excel, можно написать следующий запрос.
SELECT * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0; Database=D:TestExcel.xls', [Лист1$]);
Вместо [Лист1$] можно в апострофах указать необходимый SQL запрос, например:
SELECT * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0; Database=D:TestExcel.xls', 'SELECT ProductName, Price FROM [Лист1$]');
Функция OPENQUERY в T-SQL
OPENDATASOURCE и OPENROWSET удобно использовать, если Вам требуется разово получить данные из определённых внешних источников, в нашем случае из Excel, но если Вам требуется постоянно обращаться к таким источникам, то имеет смысл создать специальный объект в SQL Server – он называется «Связанный сервер» (Linked Server). Данный объект будет настроен на конкретный источник, и к этому объекту Вы сможете обращаться практически как к обычной базе данных, не указывая при этом строку подключения.
Связанный сервер настраивается один раз, иными словами, при его создании Вы указываете все необходимые настройки, а затем просто обращаетесь к нему по имени, которое Вы укажете.
Для обращения к связанным серверам в SQL сервере как раз и используется функция OPENQUERY.
OPENQUERY – функция обращается к связанному серверу и выполняет указанный запрос. На эту функцию можно даже ссылаться в инструкциях по модификации данных, т.е. мы можем изменять данные на связанном сервере.
Связанный сервер сначала нужно создать, это можно сделать с помощью системной процедуры sp_addlinkedserver, доступ к данному серверу настраивается процедурой sp_addlinkedsrvlogin. Связанные серверы также можно создавать и с помощью среды SQL Server Management Studio (Обозреватель объектов-> Объекты сервера-> Связанные серверы).
Пример.
--Создание связанного сервера EXEC dbo.sp_addlinkedserver @server = 'TEST_EXCEL', @srvproduct='OLE DB', @provider='Microsoft.Jet.OLEDB.4.0', @datasrc='D:TestExcel.xls', @provstr='Excel 8.0' --Настройки безопасности (авторизации) EXEC dbo.sp_addlinkedsrvlogin @rmtsrvname= 'TEST_EXCEL', @useself= 'False', @locallogin=NULL, @rmtuser=NULL, @rmtpassword=NULL
Более подробно про связанные серверы можете почитать в материале «Связанные серверы в MS SQL Server – создание и примеры использования».
После того как связанный сервер создан, в моем примере я его назвал TEST_EXCEL, мы можем к нему обратиться с помощью функции OPENQUERY.
SELECT * FROM OPENQUERY (TEST_EXCEL, 'SELECT * FROM [Лист1$]')
Также к связанному серверу можно обратиться и без использования функции OPENQUERY, например:
SELECT * FROM TEST_EXCEL...[Лист1$]
Но рекомендовано к связанным серверам обращаться через функцию OPENQUERY.
Как я уже отмечал, функцию OPENQUERY можно использовать и для модификации данных на связанном сервере в инструкциях INSERT, UPDATE или DELETE, например, для обновления данных, в нашем случае инструкция будет выглядеть следующем образом.
UPDATE OPENQUERY (TEST_EXCEL, 'SELECT * FROM [Лист1$]') SET Price = 700 WHERE ProductId = 3; SELECT * FROM OPENQUERY (TEST_EXCEL, 'SELECT * FROM [Лист1$]')
Заметка! Если Вас интересует SQL и T-SQL, рекомендую посмотреть мои видеокурсы по T-SQL, с помощью которых Вы «с нуля» научитесь работать с SQL и программировать с использованием языка T-SQL в Microsoft SQL Server.
У меня на этом все, надеюсь, материал был Вам полезен, пока!
Table of Contents
- Introduction
- Building the Environment for Testing
- Creating an Excel File to test
- Installing the necessary components in Windows Server
- Enabling SQL Server Instance to Read File
- Querying and Importing the Spreadsheet
- Conclusion
- References
- See Also
- Other Languages
Introduction
We often have to perform data integration in SQL Server, with different data sources such as «.txt» files (tabular text or with separator character), «.csv» files or «.xls» (Excel) files.
It is always not possible to create a SSIS package to do this data import, a useful alternative is to use OPENROWSET method for importing data.
In this article, we will use data import from Excel files (.xls e .xlsx).
Building the Environment for Testing
So that we see the data import process steps from an Excel file to a table from database, we need:
- Create an Excel file to import sample;
- Configure Windows Server, installing the necessary components;
- Configure the necessary permissions to the SQL instance that we need to obtain data files.
Let’s prepare environment for data import!
Creating an Excel File to test
In this step, we will create an Excel file sample with just a few rows to demo.
Add a header row, to explicitly define the data: «ID», «Item Name» and «Date Created».
The data sequences is only to facilitate the visualization of the content that is being manipulated.
See this Excel file in the image below (click to enlarge)
Installing the necessary components in Windows Server
To get the data through a query inside SQL Server, use an OLE DB Data Provider.
Most files can now use the
Microsoft.ACE.OLEDB.12.0 Data Provider that can be obtained free through Data Connectivity Components.
This package will provide all ODBC and OLEDB drivers for data manipulation, as follow below:
File Type (extension) | Extended Properties |
Excel 97-2003 Workbook (.xls) | Excel 8.0 |
Excel 2007-2010 Workbook (.xlsx) | Excel 12.0 XML |
Excel 2007-2010 Macro-enabled workbook (.xlsm) | Excel 12.0 Macro |
Excel 2007-2010 Non-XML binary workbook (.xlsb) | Excel 12.0 |
There are two versions of this package: «AccessDatabaseEngine.exe» for x86 platform and other «AccessDatabaseEngine_x64.exe» for x64 platform.
The minimum system requirements for this installation can be obtained in the same
download package page.
If you are installing the x86 package you must ensure that your user is allowed access to the Temporary directory of your Windows OS.
To know what your Temporary directory open the «Control Panel», click «Advanced System Settings» option. A window will open, select the «Advanced» tab and click the «Environment Variables» button.
A new window will open with your environment variables, including «TEMP» and «TMP» variables, indicating your Temporary directory.
See this windows in the image below (click to enlarge)
So if your operating system is Windows 32-bit (x86) is necessary to include read and write access to the user of your SQL Server instance.
It’s important to remember that the user of your SQL Server instance must be a local user or the default «Local System» account to grant this access.
See this window Service Properties in the image below
Enabling SQL Server Instance to Read File
The settings and permissions to execute a query external data has some details that should be performed to be able to get the data from an Excel files (.xls ou .xlsx) and also other formats.
The execution of distributed queries as OPENROWSET is only possible when the SQL Server instance has the
Ad Hoc Distributed Queries configuration enabled. By default, every SQL Server instance maintains this permission denied.
Note |
---|
The Advanced Settings should only be changed by an experienced professional or a certified professional in SQL Server. It’s important to note not use these commands in Production Databases without previous analysis. |
To enable this feature just use the sp_configure system stored procedure in your SQL instance to display its Advanced Settings in
show advanced options parameter and soon to follow, enable the Ad Hoc Distributed Queries setting to enabling the use of distributed queries.
USE [master]
GO
—CONFIGURING SQL INSTANCE TO ACCEPT ADVANCED OPTIONS
EXEC
sp_configure ‘show advanced options’, 1
RECONFIGURE
GO
—ENABLING USE OF DISTRIBUTED QUERIES
EXEC
sp_configure ‘Ad Hoc Distributed Queries’, 1
RECONFIGURE
GO
These changes in the Advanced settings only take effect after the execution of the RECONFIGURE command.
To get permission granted to use the Data Provider through sp_MSset_oledb_prop system stored procedure to link Microsoft.ACE.OLEDB.12.0 in
SQL Server using AllowInProcess parameter so we can use the resources of the Data Provider and also allow the use of dynamic parameters in queries through of
DynamicParameters parameter for our queries can use T-SQL clauses.
USE [master]
GO
—ADD DRIVERS IN SQL INSTANCE
EXEC
master.dbo.sp_MSset_oledb_prop
N’Microsoft.ACE.OLEDB.12.0′,
N’AllowInProcess’, 1
GO
EXEC
master.dbo.sp_MSset_oledb_prop
N’Microsoft.ACE.OLEDB.12.0′,
N’DynamicParameters’, 1
GO
See
this output SQL script in the image below
After setting up your SQL instance to use the
Microsoft.ACE.OLEDB.12.0 Data Provider and make the appropriate access permissions, we can implement the distributed queries of other data sources,
in this case to Excel files.
Querying and Importing the Spreadsheet
As this demo is for Excel files (.xls) we will perform a query using an OPENROWSET method with the Excel test file that was created earlier in this article.
We use some parameters for this method to be able to data query:
- Data Provider — In this case, using Microsoft.ACE.OLEDB.12.0
- BULK Options — File Version;Where it’s stored; Header (HDR); Import Mode (IMEX)
- Query —
T-SQL statement with or without clauses to data filter and process.
—CONSULTING A SPREADSHEET
SELECT
* FROM
OPENROWSET(‘Microsoft.ACE.OLEDB.12.0’,
‘Excel 12.0; Database=C:MicrosoftTest.xls; HDR=YES; IMEX=1’,
‘SELECT * FROM [Plan1$]’)
GO
See
this output SQL script in the image below
To data group and perform other tasks for data manipulation, the ideal is always load the data into the database. You can insert data into an existing table using the INSERT statement or you can create a table through of INTO command in SELECT statement.
—CONSULTING A SPREADSHEET
SELECT *
INTO
TB_EXAMPLE
FROM OPENROWSET(‘Microsoft.ACE.OLEDB.12.0’,
‘Excel 12.0; Database=C:MicrosoftTest.xls; HDR=YES; IMEX=1’,
‘SELECT * FROM [Plan1$]’)
GO
SELECT * FROM TB_EXAMPLE
GO
See
this output SQL script in the image below
It’s also important to check if the SQL Server Service user has access in Windows directory where Excel files
are stored.
Conclusion
Have the possibility to use an alternative resource for importing data with T-SQL command is very useful, especially when we have to manipulate files in proprietary formats, as for .xlsx files where it’s necessary to use the Data Provider appropriate to obtain
the data correctly and with ease use.
It’s important to watch out that only users that have actually need to manipulate these files can use these resources, while minimizing the vulnerability of their environment through a permission in your SQL Server.
References
- OPENROWSET (Transact-SQL)
- Import Bulk Data by Using BULK INSERT or OPENROWSET(BULK…) (SQL Server)
- OLE DB Providers Tested with SQL Server
- Excel Source
See Also
- Transact-SQL Portal
- Wiki: Portal of TechNet Wiki Portals
Other Languages
- Importando uma planilha Excel para um Banco de Dados SQL Server (pt-BR)
This article was awarded the silver medal in the TechNet
Guru of April 2014
There are plenty of examples around showing how to use OPENROWSET and OPENDATASOURCE to open queries against tables on remote SQL servers, and to get data from files on other servers. Unfortunately, none of them tell me how to do the specific thing that I need to do, which is use one of these commands to get either a .csv or .xlsx Excel file from a remote server using the server IP address and windows login where the server has no SQL Server instance installed — only either IIS7 or 8.
It would be great if Microsoft documentation didn’t omit basic examples of how to use their tools in the most likely of ways. It takes no real intelligence to try 40 odd different combinations of parameters list: it’s just an inefficient waste of time. Developers have more important things to do than waste hours trying to discover some ‘secret knowledge’ recipe which is really just a not-properly-documented variant of the command parameters, and pretending that this is something to do with being clever.
So to assuage my philosophical angst, could someone please provide an example of how to use either OPENROWSET or OPENDATASOURCE to get/select content from either a .csv file or an excel spreadsheet using the remote server IP address XXX.XXX.XXX.XXX, a port number if it should be included, the file system path correctly appended to that or included in the command, and the correct arrangement of the username and password parameters. There is no SQL server instance on the remote server — just IIS and a website with windows auth. If you have an example that works with SQL server instance on the remote server, that will do (although I think some of the REMOTEDATASOURCE examples cover this already) but I REALLY want an example where I don’t have to have an SQL server instance on the remote web server.
I know that you can use linked servers for this, but there is some extra baggage to put on the remote server containing the file to access, and in my case the server containing the excel or .csv text file will not even have a SQL Server instance on it — only IIS and a website.
(Also — can someone confirm whether you can use linked servers with NO SQL Server or other database server instance on the remote server with the desired text data file? Is it linked database servers only, or linked servers where the remote server can just be a windows and web server with no SQL server installed on it?)
This example is handy, but does not tell me if SERVERPROPERTY('MachineName') AS nvarchar(128))
can contain an IP address of a remote windows server with no SQL server instance on it, and it is not about accessing text files.
This example comes blisteringly close, but there does not seem to be a positive outcome in the post, and there is no text file involved — only a DB query so the remote system has SQL server installed?:
SELECT *
FROM OPENROWSET('PACISOleDb', '192.168.200.172';'admin';'admin', 'SELECT * FROM DB')
Existing examples that are good but don’t help much:
SELECT *
FROM OPENROWSET('SQLNCLI',
'DRIVER={SQL Server};SERVER=YourServer;UID=UserID;PWD=Password',
'select * from sys.sysobjects')
(Source)
SELECT ContactName, CompanyName, ContactTitle
FROM OPENDATASOURCE('SQLOLEDB',
'Data Source=ServerName;User ID=MyUID;Password=MyPass' ).Northwind.dbo.Customers
(source)
-- SQL Server 2012
SELECT
*
FROM
OPENDATASOURCE ('SQLNCLI11',
'Data Source=SQLInstanceName;Catalog=DBName;User ID=SQLLogin;Password=Password;').DBName.SchemaName.TableName
SELECT *
FROM OPENROWSET('SQLNCLI11',
'DRIVER={SQL Server};SERVER=SQLInstanceName;UID=SQLLogin;PWD=Password',
'select * from DBName..TableName')
--Access DB
SELECT * FROM OPENDATASOURCE ('Microsoft.ACE.OLEDB.12.0',
'Data Source=D:MyDBMyAccessDB.accdb')...TableName
(source)
SELECT *
FROM OPENROWSET('SQLNCLI',
'DRIVER={SQL Server};SERVER=MyServer;UID=MyUserID;PWD=MyCleverPassword',
'select @@ServerName')
(source. I take it that {SQL Server} here has to be a SQL server name — not a remote IP Address? It is not clear.)
SELECT *
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;Database=C:test.xls;HDR=No',
'SELECT * FROM [Sheet1$]') b
(source)
--Excel 2007-2010
SELECT * --INTO #productlist
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0 Xml;HDR=YES;Database=C:tempProducts.xlsx',
'SELECT * FROM [ProductList$]');
--Excel 97-2003
SELECT * --INTO #productlist
FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0',
'Excel 8.0;HDR=YES;Database=C:tempProducts.xls',
'select * from [ProductList$]');
(source. Again — no remote server, no IP server address, no credentials)
Lots to chose from here. GREAT blog post, but not useful for my specific ends:
SELECT *
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0 Xml;
Database=C:DataFilesEmployeeData1.xlsx',
[vEmployee$]);
SELECT *
FROM OPENDATASOURCE('Microsoft.ACE.OLEDB.12.0',
'Data Source=C:DataFilesEmployeeData1.xlsx;
Extended Properties=Excel 12.0 Xml')...[vEmployee$];
SELECT *
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0 Xml; HDR=YES;
Database=C:DataFilesEmployeeData1.xlsx',
[vEmployee$]);
SELECT *
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0 Xml; HDR=NO;
Database=C:DataFilesEmployeeData1.xlsx',
[vEmployee$]);
SELECT * INTO EmployeeInfo3
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0 Xml; HDR=YES; IMEX=1;
Database=C:DataFilesEmployeeData1.xlsx',
[vEmployee$]);
(source)
The MS openrowset documentation has an ‘|’ in the command schema suggesting this cannot be done remotely:
SELECT *
FROM OPENROWSET(BULK N'D:XChangetest-csv.csv',
FORMATFILE = N'D:XChangetest-csv.fmt',
FIRSTROW=2,
FORMAT='CSV') AS cars;
H. Accessing data from a CSV file without a format file:
tsql
Copy
SELECT * FROM OPENROWSET(
BULK 'C:Program FilesMicrosoft SQL ServerMSSQL14.CTP1_1MSSQLDATAinv-2017-01-19.csv',
SINGLE_CLOB) AS DATA;
So I am thinking that the correct OR ONLY approach involves OPENDATASOURCE with the jet driver present and accessible on the remote server. However, where in the following example from MS does the IP address of the remote server and the login password and username go? If it cannot then that seems contradictory to the claims of the capability of the command in the documentation (based on the words they use), and it would be great if they could say ‘you cannot do this’, as it is pretty clear that most people will try…
SELECT * FROM OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0',
'Data Source=C:DataFolderDocumentsTestExcel.xls;Extended Properties=EXCEL 5.0')...[Sheet1$] ;
So,
Something like:
SELECT * FROM OPENDATASOURCE('Microsoft.ACE.OLEDB.12.0'|'Microsoft.Jet.OLEDB.4.0'|'PACISOleDb', 'Data Source=XXX.XXX.XXX.XXXDataFolderDocumentsTestExcel.xls';User ID=MyUID;Password=MyPass;Extended Properties=EXCEL 5.0')...[Sheet1$] ;
or maybe
(TWIDDLING THUMBS — BORING BORING BORING)
I should give up and use a linked server:
EXEC sp_addlinkedserver
@server = 'ExcelLinkSrv1',
@srvproduct = 'Excel',
@provider = 'Microsoft.ACE.OLEDB.12.0',
@datasrc = 'C:DataFilesEmployeeData1.xlsx',
@provstr = 'Excel 12.0 Xml; HDR=YES; IMEX=1';
GO
But again — can I have:
@server = ‘202.217.XXX.XXX’
?
Yes — I know that you would normally not hard code it, but let’s start simple in SSMS. I wanted to avoid linked server for different reasons. How do you do it with OPENROWSET or else OPENDATASOURCE against excel spreadsheet or else .csv file?
You can have all of my reputation points or whatever they are if your solution or information works (and someone else did not get them first), because I don’t care about that stuff.
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:
OPENROWSET
( { 'provider_name' , { 'datasource' ; 'user_id' ; 'password'
| 'provider_string' }
, { [ catalog. ] [ schema. ] object
| 'query'
}
| BULK 'data_file' ,
{ FORMATFILE = 'format_file_path' [ <bulk_options> ]
| SINGLE_BLOB | SINGLE_CLOB | SINGLE_NCLOB }
} )
<bulk_options> ::=
[ , CODEPAGE = { 'ACP' | 'OEM' | 'RAW' | 'code_page' } ]
[ , DATASOURCE = 'data_source_name' ]
[ , ERRORFILE = 'file_name' ]
[ , ERRORFILE_DATASOURCE = 'data_source_name' ]
[ , FIRSTROW = first_row ]
[ , LASTROW = last_row ]
[ , MAXERRORS = maximum_errors ]
[ , ROWS_PER_BATCH = rows_per_batch ]
[ , ORDER ( { column [ ASC | DESC ] } [ ,...n ] ) [ UNIQUE ] ]
-- bulk_options related to input file format
[ , FORMAT = 'CSV' ]
[ , FIELDQUOTE = 'quote_characters']
[ , FORMATFILE = 'format_file_path' ]
Supported Data Providers include:
- SQLNCLI for accessing remote SQL Server Databases
- Microsoft.Jet.OLEDB.4.0 for accessing Microsoft Access Databases and Excel Workbooks
- Microsoft.ACE.OLEDB.12.0 for accessing Microsoft Access Databases and Excel Workbooks
The older JET data provider is only available for 32 bit platforms, but ACE is available for 64 bit platforms as well.
ACE is an abbreviation for Access Connectivity Engine, but is now generally known as the Access Database Engine. ACE is backewardly compatible with JET so can be used to query older versions of Access databases and Excel workbooks.
ACE is going to be used in this article as it is the more modern and flexible implementation.
We will us a sample Workbook that contains some Order Records:
Obviously we could import this data into a SQL Server database, but if the data is generated by a Legacy system that is limited in export capabilities, or a person is manually constructing the data set, and it frequently changes and is replaced it may be more practical to simply query the data in place.
As already mentioned the ACE library can be used to query the data in an Excel Workbook, using the OPENROWSET function.
The following example retrieves all columns for all records in an Excel workbook called Northwind Orders.xls, on a worksheet called Orders.
USE Northwind
GO
SELECT *
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0','Excel 12.0;Database=e:TestZoneNorthwind Orders.xls;HDR=YES',Orders$)
The arguments to the OPENROWSET function to connect to the Excel Worksheet data are:
- Data Provider: ACE (Microsoft.ACE.OLEDB.12.0)
- Workbook version: Excel 2007 format (Excel 12.0)
- Workbook: e:TestZoneNorthwind Orders.xls
- Woksheet Range Name: Orders$
The results from this query are as follows:
Joining OPENROWSET results sets to Database Tables with JOINs
The OPENROWSET can be used in a JOIN clause.
--Join to other tables
SELECT wb.[Order ID], wb.Customer, wb.Employee, wb.[Order Date], od.ProductID, od.Quantity
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0','Excel 12.0;Database=e:TestZoneNorthwind Orders.xls;HDR=YES',Orders$) AS wb
--Orders$ is a named range for a worksheet called Orders. This could be any named range in the workbook.
JOIN [Order Details] As od ON wb.[Order ID] = od.OrderID
This query pulls four columns from the workbook and one from the table called [Order Details] in a database called Northwind. Mote the alias of wb created for the “table” created by the OPENROWSET query. This is not required, but makes it easier to reference and identify columns when multiple tables are in the query.
I hope you have found this helpful. Do share this article on Social Media if you like it.