Export from excel to sql database

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.

  1. In [!INCLUDEssManStudioFull], connect to an instance of the [!INCLUDEssNoVersion] [!INCLUDEssDE].

  2. Expand Databases.

  3. Right-click a database.

  4. Select Tasks.

  5. 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

There are many ways to import data from an Excel file to a SQL Server database using:

  • SQL Server Integration Services (SSIS)
  • the OPENROWSET and OPENDATASOURCE functions
  • SQL Server linked servers
  • the SQL Server Import and Export Wizard

In this article, steps for importing data from an Excel file to a SQL Server database will be explained using the SQL Server Import and Export Wizard including some of problems that can occur during the processes.

To start the process of importing data from an Excel file to a SQL Server database using the SQL Server Import and Export Wizard the SQL Server Import and Export Wizard needs to be launched. There are several ways to do that, and this can be seen on the How to import/export data to SQL Server using the SQL Server Import and Export Wizard page

The first page that appears when the SQL Server Import and Export Wizard launched is Welcome page:

On this page, only a quick introduction of the SQL Server Import and Export Wizard is shown.

Click the Next button to continue. The next page in the SQL Server Import and Export Wizard is Choose a Data Source page:

In the Choose a Data Source page, in order to continue with importing data from Excel to SQL Server the data source provider and way of connecting with data source must be provided. In our case, the provider for connecting to the Excel file is the Microsoft Excel provider.

From the Data source drop down box, select the Microsoft Excel provider:

As you can see, there is no Microsoft Excel provider in the list of the Data source drop down box. There are several reasons for this. The first reason could be that Microsoft Office isn’t installed. But there is no need to install Microsoft Office (Microsoft Excel) in order to see the Microsoft Excel provider in the Data source drop down box list.

To see the Microsoft Excel provider in the list, install Microsoft Access Database Engine 2016 Redistributable. The latest version of Microsoft Access Database Engine can open the earlier version of Excel, so make sure that you have the latest one.

The Microsoft Access Database Engine 2016 Redistributable comes with two versions:

  • AccessDatabaseEngine.exe is 32-bit version
  • AccessDatabaseEngine_X64.exe is 64-bit version

Now, when Microsoft Access Database Engine 2016 Redistributable is installed, we should see the Microsoft Excel provider in the list, but unfortunately the Microsoft Excel provider does not show in the list of the Data source drop down box.

This is because you may run a wrong version of the SQL Server Import and Export Wizard. For example, the AccessDatabaseEngine.exe is installed and the SQL Server Import and Export Wizard 64-bit version is launched. In this case, in order to see the Microsoft Excel provider in the list of the Data source drop down box, launch the SQL Server Import and Export Wizard 32-bit version and the Microsoft Excel provider will appear in the list:

Now, when all is installed, from the list, select the Microsoft Excel provider. On the Choose a Data Source page, additional options appear:

In the Excel file path box, type the location to the Excel file or use the Browse button to navigate to the location:

From the Excel version drop down box, choose the version of Excel that uses the source workbook. In our case, that is the Microsoft Excel 2016 version:

The latest option on this page is the First row has column names check box:

By default, this option is checked. It treads the first rows of the data source as the column names:

But if this option is enabled and data source does not contain column names, the SQL Server Import and Export Wizard will add the column names, starting with the name F1 for the first column name:

If this option is disabled and data source contain the column names the SQL Server Import and Export Wizard treats these columns as the first row of data:

Now, when everything is set on the Choose a Data Source page of the SQL Server Import and Export Wizard, click the Next button to continue.

The following warning message may appear after clicking the Next button:

The operation could not be completed.

Additional information:

The ‘Microsoft.ACE.OLEDB.16.0’ provider is not registered on the local machine. (System.Data)

Typically, this warning message appears when the SQL Server Import and Export Wizard is launched via SQL Server Management Studio (SSMS) which is a 32-bit application and the 32-bit version of the SQL Server Import and Export Wizard is launched, but you have installed the Microsoft Access Database Engine 2016 Redistributable 64 bit version.

There are two solutions for resolving this problem:

  • The first is to launch the 64-bit version of the SQL Server Import and Export Wizard via the Start menu
  • The second resolution is to install the Microsoft Access Database Engine 2016 Redistributable 32 bit version and continue using the SQL Server Import and Export Wizard via SSMS, but first, you need to uninstall the 64-bit version of Microsoft Access Database Engine 2016 Redistributable, otherwise the following warning message will appear when launching the installation package:

Note: The Microsoft Access Database Engine 2016 Redistributable can be installed in quiet mode.

Open the Command Prompt window and run the following:

For 32-bit version


C:Users<User_Name>DownloadsAccessDatabaseEngine.exe /quiet

For 64-bit version


C:Users<User_Name>DownloadsAccessDatabaseEngine_X64.exe /quiet

The next page on the SQL Server Import and Export Wizard is the Choose a Destination page:

On this page, determine the destination where data from the data source (Excel file) will be placed. In our case, the destination will be a SQL Server database.

Under the Destination drop down box, choose a provider that can connect to a SQL Server database.

One of providers that can connect to SQL Server is:

  • .NET Framework Data Provider for SqlServer
  • Microsoft OLE DB Provider for SQL Server
  • SQL Server Native Client 11.0

In this case, the SQL Server Native Client 11.0 will be selected from the Destination list:

From the Server name combo box, select the SQL Server instance:

In the Authentication section, determine how it will be connected to SQL Server by using Windows or SQL Server Authentication mode.

From the Database drop down box, pick a database in which data from data source (Excel file) will be placed:

Or, create a new database as a destination for data from data source.

To do that, click the New button and in the Create Database dialog, set the parameters for the new SQL Server destination database:

When everything is set on the Choose a Destination page, click the Next button to proceed.

On the Specify Table Copy or Query page, determine how data from the data source will be copied to the destination:

If the Copy data from one or more tables or views radio button is selected then all data from the chosen worksheets will be copied.

If the Write a query to specify the data to transfer is chosen, then only data that are specified in a SQL query will be copied from an Excel worksheet to a destination SQL Server database.

If the Write a query to specify the data to transfer in the Choose a Destination page is chosen, then the Provide a Source Query page will be shown when the Next button is pressed:

In the SQL statement text box, type the query that will select the data to copy form the Excel file to the SQL Server database or load a query using the Browse button.

To successfully query a worksheet the $ to the end of the sheet name and the brackets around sheet name, ([BusinessEntity$]) must be added, otherwise the following warning messages may appear:

The statement could not be parsed.

Additional information:

The Microsoft Access database engine could not find the object ‘BusinessEntity’. Make sure the object exists and that you spell its name and the path name correctly. If ‘BusinessEntity’ is not a local object, check your network connection or contact the server administrator. (Microsoft Access Database Engine)

Or this:

The statement could not be parsed.

Additional information:

Syntax error in FROM clause. (Microsoft Access Database Engine)

If the Copy data from one or more tables or views radio button is chosen, when the Next button is pressed, the Select Source Tables and Views page will be shown:

On this page, all worksheets for the Excel file (ImportData.xlsx) will be listed under the Source column. From the Source list, choose from which worksheets you want to import data to the SQL Server database by clicking the check box next to the name of the worksheets. The selected worksheets will appear in the Destination column:

The name of the tables in the SQL Server database by default will be the names of the selected worksheets from the Source column, but these names can be changed by clicking on the name in the Destination column:

As it may be noticed, the icon in the Destination column for the BusinessEntity$ field is different from the Table1 and Table2 fields. This is because the table in the SQL Server database that is chosen as a destination already exists and for the Table1 and Table2 fields, new tables will be created.

When using an existing table, make sure that the destination table has the same number of columns that have data source, otherwise the columns from the data source that does not have an adequate destination column will be by default ignored (<ignore>) and the data from that columns will not be imported to a destination table:

Also, make sure that columns in the destination table have data types that are compatible with the data types in the columns of the source data, otherwise the following error may appear:

Found 1 unknown column type conversion(s) You are only allowed to save the package

When the appropriate columns and the data types are set, click the Next button, the Save and Run Package page of the SQL Server Import and Export Wizard will appear:

Click the Next button if want to import data from an Excel file to a SQL Server database, but if want to save the SSIS package for the later use, check the Save SSIS Package button.

On the Complete the Wizard page, choices that are made in the previous wizard pages are shown:

Click the Next button to import data from an Excel file to a SQL Server database. The Performing Operation page shows the status of the importing process if it is finished successfully or with the errors:

If the error occurs during the process of importing data from Excel file to the SQL Server database the Messages… word will appear in the Message column for the action that failed:

When click on the Messages…, the View Report dialog will appear with detailed information about the error:

This error:

– Validating (Error)

Messages

  • Error 0xc0202049: Data Flow Task 1: Failure inserting into the read-only column “numb”.
    (SQL Server Import and Export Wizard)
  • Error 0xc0202045: Data Flow Task 1: Column metadata validation failed.
    (SQL Server Import and Export Wizard)
  • Error 0xc004706b: Data Flow Task 1: “Destination – test1” failed validation and returned validation status “VS_ISBROKEN”.
    (SQL Server Import and Export Wizard)
  • Error 0xc004700c: Data Flow Task 1: One or more component failed validation.
    (SQL Server Import and Export Wizard)
  • Error 0xc0024107: Data Flow Task 1: There were errors during task validation.
    (SQL Server Import and Export Wizard)

Typically, it appears when the destination table has an IDENTITY column. To resolve this, turn back to the Select Source Tables and Views page, select the tables that have identity property and press the Edit Mappings button. From the Transfer Settings dialog, select the Enable identity insert check box:

Also, another common problem that may appear when importing data from data source to the destination SQL Server tables is the FOREIGN KEY Constraints problem. In the error below two destination tables test1 and test2 are shown. The test2 table is referenced to the test1 table:

– Copying to [dbo].[test1] (Error)

Messages

  • Error 0xc0047022: Data Flow Task 1: SSIS Error Code DTS_E_PROCESSINPUTFAILED. The ProcessInput method on component “Destination 1 – test2” (79) failed with error code 0xC0209029 while processing input “Destination Input” (92). The identified component returned an error from the ProcessInput method. The error is specific to the component, but the error is fatal and will cause the Data Flow task to stop running. There may be error messages posted before this with more information about the failure.
    (SQL Server Import and Export Wizard)
  • Information 0x402090df: Data Flow Task 1: The final commit for the data insertion in “Destination – test1” has started.
    (SQL Server Import and Export Wizard)
  • Information 0x402090e0: Data Flow Task 1: The final commit for the data insertion in “Destination – test1” has ended.
    (SQL Server Import and Export Wizard)

– Copying to [dbo].[test2] (Error)

Messages

  • Error 0xc0202009: Data Flow Task 1: SSIS Error Code DTS_E_OLEDBERROR. An OLE DB error has occurred. Error code: 0x80004005.
    An OLE DB record is available. Source: “Microsoft SQL Server Native Client 11.0” Hresult: 0x80004005 Description: “The statement has been terminated.”.
    An OLE DB record is available. Source: “Microsoft SQL Server Native Client 11.0” Hresult: 0x80004005 Description: “Cannot insert the value NULL into column ‘No_id’, table ‘ImportData.dbo.test2’; column does not allow nulls. INSERT fails.”.
    (SQL Server Import and Export Wizard)
  • Error 0xc0209029: Data Flow Task 1: SSIS Error Code DTS_E_INDUCEDTRANSFORMFAILUREONERROR. The “Destination 1 – test2.Inputs[Destination Input]” failed because error code 0xC020907B occurred, and the error row disposition on “Destination 1 – test2.Inputs[Destination Input]” specifies failure on error. An error occurred on the specified object of the specified component. There may be error messages posted before this with more information about the failure.
    (SQL Server Import and Export Wizard)

To resolve this problem, disable constraint for referenced table (test2) by executing the following code in SSMS:

ALTER TABLE test2 NOCHECK CONSTRAINT ALL

After importing data from data source to the destination SQL Server database, execute the following code in SSMS to enable constraint for the test2 table:

ALTER TABLE test2 WITH CHECK CHECK CONSTRAINT ALL

In some cases, the warning message like from the image below may appear:

– Validating (Warning)

Messages

  • Warning 0x802092a7: Data Flow Task 1: Truncation may occur due to inserting data from data flow column “AddressLine1” with a length of 255 to database column “AddressLine1” with a length of 60.
    (SQL Server Import and Export Wizard)
  • Warning 0x802092a7: Data Flow Task 1: Truncation may occur due to inserting data from data flow column “AddressLine2” with a length of 255 to database column “AddressLine2” with a length of 60.
    (SQL Server Import and Export Wizard)
  • Warning 0x802092a7: Data Flow Task 1: Truncation may occur due to inserting data from data flow column “City” with a length of 255 to database column “City” with a length of 30.
    (SQL Server Import and Export Wizard)
  • Warning 0x802092a7: Data Flow Task 1: Truncation may occur due to inserting data from data flow column “AddressLine1” with a length of 255 to database column “AddressLine1” with a length of 60.
    (SQL Server Import and Export Wizard)
  • Warning 0x802092a7: Data Flow Task 1: Truncation may occur due to inserting data from data flow column “AddressLine2” with a length of 255 to database column “AddressLine2” with a length of 60.
    (SQL Server Import and Export Wizard)
  • Warning 0x802092a7: Data Flow Task 1: Truncation may occur due to inserting data from data flow column “City” with a length of 255 to database column “City” with a length of 30.
    (SQL Server Import and Export Wizard)
  • Warning 0x80049304: Data Flow Task 1: Warning: Could not open global shared memory to communicate with performance DLL; data flow performance counters are not available. To resolve, run this package as an administrator, or on the system’s console.
    (SQL Server Import and Export Wizard)

To resolve this, go to the SQL Server destination table and increase the column size for the columns that are listed in the warning message.

To verify that the data from the Excel file are imported to the SQL Server database, go to SSMS, find the database in which data are imported and list all data from the tables:

References:

  • Import data from Excel to SQL Server or Azure SQL Database
  • How to import/export data to SQL Server using the SQL Server Import and Export Wizard
  • How to query Excel data using SQL Server linked servers
  • Import and Export Data with the SQL Server Import and Export Wizard
  • Author
  • Recent Posts

Marko Zivkovic

Marko aka “Zivko” is a senior software analyst from Nis, Serbia focusing on SQL Server and MySQL as well as client technologies like SSMS, Visual Studio, and VSCode. He has extensive experience with quality assurance, issue escalation/resolution, and product evangelism.

He is a prolific author of authoritative content related to SQL Server including a number of “platinum” articles (top 1% in terms of popularity and engagement). His writing covers a range of topics on MySQL and SQL Server including remote/linked servers, import/export, LocalDB, SSMS, and more.

In his part-time, Zivko likes basketball, foosball (table-soccer), and rock music.

See more about Marko at LinkedIn

View all posts by Marko Zivkovic

Marko Zivkovic

Importing data in SQL database is playing an important role when working with SQL servers. There are various techniques and tools to facilitate data entry into the SQL database. This article will explain how to import data in an SQL database from an Excel file by using two methods:

  • SQL Server Management Studio (SQL Server Import and Export data wizard)
  • ApexSQL Pump

Import data in SQL database via SQL Server Import and Export data wizard

SQL Server Management Studio allows users to import data from different data sources, which will be explained in this chapter.

On SQL Server Management Studio launch, the Connect to Server window will be opened. Choose the Server name and the type of Authentication, provide credentials, and click the Connect button:

The Connect to the SQL server window

When SSMS is connected to the chosen instance of SQL Server, right-click on the desired database and navigate to Tasks > Import data option from the Tasks submenu:

Import data option in the Tasks submenu

That action will open the SQL Server Import and Export Wizard window. The first step of this process gives us a brief overview of what the wizard does. It’s designed to help users import and export data between many popular data formats including databases, spreadsheets, and text files. By clicking on the Next button, the wizard will go to the next step:

The Welcome to SQL Server Import and Export Wizard window

The Choose a Data Source step will be the next on the journey through import data in SQL database with this wizard. The source from which data will be copied will be selected in this step.

From the Data source drop-list list, choose Microsoft Excel as the source. The section below will be changed with options following the selected data source. In the Excel connection settings section, the path to an Excel file will be chosen by clicking on the Browse button:

The Choose a Data Source window

The data from the dbo.Export_data Excel file will be used for importing in the desired database:

The dbo.Export_data Excel file

When the data source is chosen, click on the Next button to continue. The following warning message might be shown:

  • The operation could not be completed.
    Additional information:
    The ‘Microsoft.ACE.OLEDB.12.0’ provider is not registered on the local machine. (System.Data)

"The operation could not be completed" warning message

This warning message is usually encountered on a 64-bit operating system in a combination with the 32-bit version of SQL Server Management Studio. To bypass this issue, close SSMS, go to the Start menu, find and open the 64-bit version of SQL Server Import and Export Wizard:

The windows start menu

Like on the 32-bit version, the same welcome step will be presented with the exact same steps. When everything previously mentioned in the article has been set, from the Choose a Data Source window click on the Next button.

Choose a destination step will be next in which the Destination where the data will be copied to will be set. From the Destination drop-down list, choose SQL Server Native Client 11.0:

The Choose a destination window

With the selected destination, the section below the Destination drop-list will change automatically. Here, the Server name, type of Authentication, and the Database need to be set. Click on the Next button when all is specified:

The Choose a destination window  with set instance of SQL Server and desired database

Moving on, in the Specify Table Copy or Query step, there are two options available:

  • Copy data from one or more tables or views
  • Write a query to specify the data to transfer

The Copy data from one or more tables or views option will be selected in this case. Click on the Next button to continue the process of importing data in SQL Database:


The Specify Table Copy or Query window

The Select Source Tables and Views step is next in this wizard. It allows users to chosen one or more tables and views to copy data. For this article, dbo.Export_data table is selected. Furthermore, it allows users to edit mappings by clicking on the Edit mappings button, and to see how the imported data will look in the database by clicking on the Preview button. Click Next to continue the process of importing data in SQL database:

The Select Source Tables and Views window

In the Save and Run Package step, the user can choose whether to save the SSIS package. The default option is Run immediately. Click on the Next button to continue the importing process:

The Save and Run Package window

Last but not least, the Complete the Wizard is the last step in the processing of import data in SQL database. Here, a summary of the choices that were made through the process of importing data is listed. Verify that everything is okay and click Finish to end the process:

The Complete the Wizard window

The execution was successful message is shown with a brief status of the done operation. Click Close to exit the wizard:

The summary step in the SQL Server Import and Export Wizard window

The targeted database will be populated with the newly created table and data as can be seen below:

The newly created table with imported data

Import data in SQL database via ApexSQL Pump

ApexSQL Pump is a database pump tool, which allows the users to easily export or import data in SQL database and reverse.

On ApexSQL Pump launch, the New project window will be shown. In the Data source tab, choose Server, the type of the Authentication, and the Database. For this article, the AW2019 database will be used. Click Next to continue:

New project window

Under the Action tab, the Import action will be selected. Click OK to connect to the targeted database:

The Action tab in the New project window

The main window with tables and views from the targeted database will appear in the main window:

The main window of ApexSQL Pump

To import data in SQL database, click on the Manage button in the Home tab:

The Manage button in the Home tab

The Manage import window will be opened. Click on the Add button in the Format tab to add the external file for import:

The Manage import window

The Add import source window will appear where users can choose between the Database or the File data source for importing. The File option will be checked for purpose of this article. When checked, and File option with the Browse for folder button will appear:

The Add import source window

Click on this option in which the file for import data in SQL database will be selected. In this case, dbo.Excel_data Excel file will be selected. Click Open to continue:

The Open dialog

Click OK to close the Add Import source window:

The Add Import source window

When the import source is added, on the right side of the Manage import window, additional options for an imported data source will appear. In our case, options for the Excel file.

From Import by section, the option for Rows will be ticked by default, and in the Header section, the First row in range option will be checked. The Preview window shows how imported data will look in real-time. When all is set, click OK to continue:

The right side of the Manage import window

The view will go back to the main grid, where columns from the chosen table will be used for mapping columns from the imported file, in this case, the dbo.Customer table will be used:

The dbo.Customer table in the main grid

The next step is to go into the Settings window on the right side of the main window.

From the Mapping drop-down list, choose the previously selected dbo.Export_data Excel file. Then under the Table section, click on the Create new option, and in the field below type the name of the new table. Last, from the Import mode section, click on the Insert new option:

The Settings window

Moving on, in the main grid, go to the selected table, and from the Column mapping drop-down list choose which columns will be paired, for example, Customer_ID column with ID column in imported file source:

The main grid

When all columns from the imported source are mapped with propriety column from the selected table, click on the Process button in the Home tab:

The Process button in the Home tab

The job summary window will be opened, whereby clicking on the Import button, import data in SQL database process will be started:

The Job summary window

The View results window with all results will be presented. This window also allows the users to Export results or Create report as shown below:

The View results window

Back to SSMS, do a Select Top 1000 Rows command from right-click in the Object Explorer to verify that the imported data in SQL database was successful:

The imported data

January 27, 2021

From your SQL Server Management Studio, you open Object Explorer, go to your database where you want to load the data into, right click, then pick Tasks > Import Data.

This opens the Import Data Wizard, which typically works pretty well for importing from Excel. You can pick an Excel file, pick what worksheet to import data from, you can choose what table to store it into, and what the columns are going to be. Pretty flexible indeed.

You can run this as a one-off, or you can store it as a SQL Server Integration Services (SSIS) package into your file system, or into SQL Server itself, and execute it over and over again (even scheduled to run at a given time, using SQL Agent).

Update: yes, yes, yes, you can do all those things you keep asking — have you even tried at least once to run that wizard??

OK, here it comes — step by step:

Step 1: pick your Excel source

enter image description here

Step 2: pick your SQL Server target database

enter image description here

Step 3: pick your source worksheet (from Excel) and your target table in your SQL Server database; see the «Edit Mappings» button!

enter image description here

Step 4: check (and change, if needed) your mappings of Excel columns to SQL Server columns in the table:

enter image description here

Step 5: if you want to use it later on, save your SSIS package to SQL Server:

enter image description here

Step 6: — success! This is on a 64-bit machine, works like a charm — just do it!!

TL;DR: Go here for a code sample.

This is long overdue post – I’m too busy these days with contract work and other commitments.

By the nature of my work, I have to do data migrations on a regular basis. Some of the migrations from one database into another, but most of the migrations are from Excel spreadsheets into a database.
A database to database migration is somewhat nerve racking – usually the structure is broken, data is dirty, fields re-purposed and all other sort of terrible things you read about on The Daily Wtf. The best so far I’ve seen are along the lines of person middle names stored in field called CompanyName2; employee information stored in table called tbl_Jobs and info about actual jobs in table tbl_Job – see the difference here? That is all to avoid the confusion!

Database-to-database migration are unique in their own kind. But I found a lot of similar things in a process of export from Excel into SQL Server. So I’ll write down these things here – for my own future reference and you, my dear reader, might find this useful as well.

Make data conversion repeatable

Depending on your situation, you might re-do the data migration once again with a new version of a spreadsheet: i.e. first time I was given an outdated version of a spreadsheet; or first import was into a UAT environment. Then a month after do the same migration into production, but with latest data.

Anyway, my main rule is to make as little modifications to the spreadsheet as possible – you can’t (easily) script these changes. And next time you do this, you’ll have to repeat them again. And you don’t want to repeat your tasks.

So I rarely do data clean-up in Excel. But if I find it easier to do in a spreadsheet, I write a VB macro and save it in a source control.

Save your migration script into version control

Chances that if you going to re-do the migration, you’ll get a different data set and a different set of issues related. So you’ll need to modify your migration script. And any piece of code modified over time should be in source control. Also if you work in a team, your team mates will be able to pull down your script.

Basic Migration Script Parts

Always set date format

SET DATEFORMAT dmy;

By default SQL Server works in US date format. Which is great if you are in US. I’m not. So I always need to specify what format I use for dates to avoid server confusion. And don’t get me started on US date format!!!

Always wrap your script in a transaction

begin transaction tran1

    -- do your migration script here

rollback transaction tran1

I always work in SQL Server Management Studio and you can select a SQL command and hit F5 to execute only the selected code. And I use this feature all the time – write a query in a file, execute only that query; adjust query; save the whole file, move next. Sometimes I forget to select a specific query and hit F5 – executing the entire script in my file by mistake. And usually my scripts are done quickly and are not meant to be executed twice or the entire file to be executed in one go. So I’ll have to do a clean-up and start the migration again in these cases. So wrapping the entire script in a transaction helps to prevent disasters, as long as you rollback the transaction.

Be careful with this one though: I’ve seen people accidentally starting transaction, but never committing it or rolling back, then starting another transaction… So if you think you’ve done a change in data, but can’t see this change in other select queries – most likely you have open transaction going (or a few nested ones). Just execute rollback transaction tran1 a few times until you see an error message.

Use #temp tables to import data from excel.

Temp tables are almost like real tables, only temporary. They are good for migration type of work.

I always create temp tables with fields as nvarchar(max). It is usually not worth to try to match the data type to what you have in Excel – most likely in the column where you always see a date, in one of the rows you’ll have something like ‘N/A’ that will throw an exception when attempted to be converted into datetime.

create table #people (
    Firstname nvarchar(max) collate SQL_Latin1_General_CP1_CI_AS,
    Surname  nvarchar(max) collate SQL_Latin1_General_CP1_CI_AS,
    Dob  nvarchar(max) collate SQL_Latin1_General_CP1_CI_AS,
    DateOfJoin  nvarchar(max) collate SQL_Latin1_General_CP1_CI_AS,
)

And include your default collation when you create the table. If collations in DB where you migrate data into does not match your temp-table collation, you won’t be able to do text matching or comparison between the two. On attempt to do a join matching on text or text-comparison with other tables, you might see this error:

Cannot resolve the collation conflict between "Latin1_General_CI_AS" and "SQL_Latin1_General_CP1_CI_AS" in the equal to operation.

If you get this, drop your temp table and re-create it with the required collation specified.

Save Excel as a tab-delimited-values file

For some reason CSV file format is the favourite in a community. But as soon as you have commas in the actual values, your process is going to be all screwed. Instead save your spreadseets as Tab-delimited: File => Save As => “Save as type” dropdown to Text (Tab delimited). This way you’ll save yourself headaches if there are commas in your data.

Migrating tab-delimited file into SQL Server takes the same effort as working with CSV:

BULK INSERT  #people
FROM 'd:MyFilesPathPersonnel_Details.txt'
WITH
( FIELDTERMINATOR = 't', ROWTERMINATOR = 'n' )
GO

Convert columns into CSV list

Use your regular expressions to convert from columns into comma-separated list. A lot of times I find useful to check data integrity with simple queries like

select * from companies where name not in ('CompanyA', 'CompanyB', 'CompanyC')

Usually these 'CompanyA', 'CompanyB', 'CompanyC' are stored in XLS in columns. So I copy-paste columns from XLS into Notepad++ and apply Regular Expression replace of (.*)rn into '1',. This will convert these:

CompanyA
CompanyB
CompanyC
CompanyD

Into these: 'CompanyA', 'CompanyB', 'CompanyC', 'CompanyD',

Only you’ll need to remove final comma and paste this into SQL Script.

Create insert SQL statements from columns

For simple XLS tables like lookup tables (Countries, Types, Product Categories) sometimes it is easier to generate a bunch of insert into statements in Notepad++. Copy-paste your columns from Excel into Notepad++

Great Ltd
BT
Tesco

and use regex insert into Companies (Name) values ('1') to convert your columns into

insert into Companies (Name) values ('Great Ltd')
insert into Companies (Name) values ('BT')
insert into Companies (Name) values ('Tesco')

Of course you’ll have to adjust your regular expression to match your needs, but these are usually simple and easy to do (because everyone has only one problem here!).

Beware that this is a manual non-repeatable step that you’ll have to re-do when (and if) re-doing the migration. Chances that this type of data getting changed is usually slim, but possible. So you’ll need to decide if this is worth doing. It is advisable to save your regexp in the same migration file as your other SQL – for your future reference.

Convert your stringy-dates to datetime

I use convert sql function to change date from nvarchar to datetime:

select 
    convert(datetime, Dob, 103) as DOB,
    convert(datetime, JoinDate, 103) as JoinDate
from #people 

This conversion works with UK `dd/mm/yyyy’ format. Go here for more date formats available in SQL Server.

Find non-dates

Excel being flexible is great and that’s why people love it. And in a column of dates you’ll always find somebody putting “last year, around March” which can never be converted to a datetime. And if you have enough rows in a table it’ll be difficult to spot these by scrolling and eye-balling.
For these cases IsDate is very useful. Here is the documentation – there are examples, go check it out.

So to spot a bad date entry you’d go

SELECT * FROM #people 
WHERE ISDATE(JoinDate) = 0 and JoinDate is not null

This would give you a list of all records with incorrect entry for JoinDate.

Find non-numbers

The same thing goes for numeric values. Use IsNumeric (documentation) to check if there are fields with values that are not correct numbers:

SELECT *
FROM #people 
WHERE ISNUMERIC(SomeNumber)<> 1

Move date from temp tables

Once you are done with clean up of temp tables, you’ll need to migrate the data into real tables. I’m sure you know how to do this, but for a reference I’ll add this here:

insert into people (
    FirstName, 
    Surname, 
    DateOfBirth, 
    DateOfJoin)
select 
    Firstname, 
    Surname, 
    convert(datetime, Dob, 103) as DateOfBirth, 
    convert(datetime, DateOfJoin, 103) as DateOfJoin,
from #people

If you notice I have put aliases for fields in select part of the statement. These don’t do anything for the insert statement. But when you have more than 10 fields to insert and not all the field names match (and they rarely do!), you’ll be grateful to yourself in a month – it’ll be easier to know what field is inserted into where.

Use IsNull

In many cases your database structure is much more rigid than excel given to you. Your application might expect date of birth always present for an employee, but excel does not always have that. For these cases you usually go back to the client and say “yo, dude! I need all the data”, and your client will be like “wha? Bro, it’ll take time. Can you deal with this without a DOB, bro? I know you can!”. And you have to deal with this crap. So you go ahead and use isnull(value, substitution) – very useful function that checks if value is null and replaces it with a substitution. I believe this is widely known and a lot of you already use it.

select 
    convert(datetime, isnull(Dob, '01/01/1900'), 103) as DateOfBirth
from #people

Use NullIf

In the case opposite from the above, sometimes I get only a whitespace in cells, where it actually should be nothing (nulls). And you really don’t want to copy noise into your clean new database. So use nullif(value, comparison) – function that returns null if value matches comparison

select 
    nullif(LeaveReason, '') as LeaveReason
from #people

Conditional Insert

If Statements are not supported in select statements in SQL Server. So you need to use Switch:

select
    case when Sex = 'Male' then 0 when sex='Female' then 1 else null end as Gender
from #people

Sample

I’ve created a sample Excel file and migrated it into a sample database with all the above tricks are applied. Go to Github for the repository.

Introduction

There are two ways to import SQL Server data into Microsoft Excel using VBA:

  1. To create a QueryTable connected to a database table using Excel or VBA.
  2. To insert database data to a range using ADO Recordset or Excel add-ins.

The QueryTable object has a native Excel feature to refresh data.

To refresh data inserted using ADO, just insert the data again.

There are two ways to export Excel data to SQL Server using VBA:

  1. To use ADO.
  2. To use Excel add-ins that allow saving data and support VBA integration.

You can download the example and continue reading when you try the code.

Download Example

The attached VBA code example works in Microsoft Excel 2003-2016.

The example works with data in Microsoft Azure SQL Database. So, you can test the solution right after download.

Before to continue

This article was written in June 2011. It contains the tested code that you can use. I have a lot of thanks.

I would like to recommend you to take a look at my e-book «Excel Applications. 10 Steps for VBA Developers

You can also download the workbook examples and the SaveToDB add-in used as a free VBA library.

With the SaveToDB add-in, you can create more functional VBA applications with fewer efforts.

For example, you can save data changes from Excel to a database using a single call like GetAddIn().Save.

E-book
E-book Examples
SaveToDB Add-In

Table of Contents

  • Introduction
  • SQL Server Data Import to Excel using QueryTable
  • SQL Server Data Import to Excel using ADO
  • SQL Server Data Import to Excel using SaveToDB Add-In
  • Excel Data Export to SQL Server using ADO
  • Excel Data Export to SQL Server using SaveToDB Add-In
  • Connection String Functions
  • Conclusion
  • See Also
  • Download

SQL Server Data Import to Excel using QueryTable

Function ImportSQLtoQueryTable

The function creates a native Excel QueryTable connected to the OLE DB data source specified by the conString parameter.

The result is nearly the same as using the standard Excel connection dialog box.

Function ImportSQLtoQueryTable(conString As String, query As String, target As Range) As Integer

    On Error Resume Next

    Dim ws As Worksheet
    Set ws = target.Worksheet

    Dim address As String
    address = target.Cells(1, 1).address

    ' Procedure recreates ListObject or QueryTable

    If Not target.ListObject Is Nothing Then     ' Created in Excel 2007 or higher
        target.ListObject.Delete
    ElseIf Not target.QueryTable Is Nothing Then ' Created in Excel 2003
        target.QueryTable.ResultRange.Clear
        target.QueryTable.Delete
    End If

    If Application.Version >= "12.0" Then        ' Excel 2007 or higher
        With ws.ListObjects.Add(SourceType:=0, Source:=Array("OLEDB;" & conString), _
            Destination:=Range(address))

            With .QueryTable
                .CommandType = xlCmdSql
                .CommandText = StringToArray(query)
                .BackgroundQuery = True
                .SavePassword = True
                .Refresh BackgroundQuery:=False
            End With
        End With
    Else                                          ' Excel 2003
        With ws.QueryTables.Add(Connection:=Array("OLEDB;" & conString), _
            Destination:=Range(address))

            .CommandType = xlCmdSql
            .CommandText = StringToArray(query)
            .BackgroundQuery = True
            .SavePassword = True
            .Refresh BackgroundQuery:=False
        End With
    End If

    ImportSQLtoQueryTable = 0

End Function

' Source: http://support.microsoft.com/kb/816562

Function StringToArray(Str As String) As Variant

    Const StrLen = 127
    Dim NumElems As Integer
    Dim Temp() As String
    Dim i As Integer

    NumElems = (Len(Str) / StrLen) + 1
    ReDim Temp(1 To NumElems) As String

    For i = 1 To NumElems
       Temp(i) = Mid(Str, ((i - 1) * StrLen) + 1, StrLen)
    Next i

    StringToArray = Temp
End Function

Code comments:

  • The query parameter can contain a SELECT or EXECUTE query.
  • The resulting data will be inserted starting from the top left cell of the target range.
  • If the target range contains a ListObject or QueryTable object, it will be deleted, and a new object will be created instead.

    If you need to change the query only, just change the QueryTable.CommandText property.
  • Pay attention to the .SavePassword = True line.

    Microsoft Excel stores passwords without encryption.

    If possible, use the trusted connection. However, it is not supported by Microsoft Azure SQL Database yet.

Test Code of SQL Server Data Import to Excel using QueryTable

Sub TestImportUsingQueryTable()

    Dim conString As String
    conString = GetTestConnectionString()

    Dim query As String
    query = GetTestQuery()

    Dim target As Range
    Set target = ThisWorkbook.Sheets(1).Cells(3, 2)

    Select Case ImportSQLtoQueryTable(conString, query, target)
        Case Else
    End Select

End Sub

SQL Server Data Import to Excel using ADO

Function ImportSQLtoRange

The function inserts SQL Server data to the target Excel range using ADO.

Function ImportSQLtoRange(conString As String, query As String, target As Range) As Integer

    On Error Resume Next

    ' Object type and CreateObject function are used instead of ADODB.Connection,
    ' ADODB.Command for late binding without reference to
    ' Microsoft ActiveX Data Objects 2.x Library

    ' ADO API Reference
    ' https://docs.microsoft.com/en-us/sql/ado/reference/ado-api/ado-api-reference?view=sql-server-ver16

    ' Dim con As ADODB.Connection
    Dim con As Object
    Set con = CreateObject("ADODB.Connection")

    con.ConnectionString = conString

    ' Dim cmd As ADODB.Command
    Dim cmd As Object
    Set cmd = CreateObject("ADODB.Command")

    cmd.CommandText = query
    cmd.CommandType = 1         ' adCmdText

    ' The Open method doesn't actually establish a connection to the server
    ' until a Recordset is opened on the Connection object
    con.Open
    cmd.ActiveConnection = con

    ' Dim rst As ADODB.Recordset
    Dim rst As Object
    Set rst = cmd.Execute

    If rst Is Nothing Then
        con.Close
        Set con = Nothing

        ImportSQLtoRange = 1
        Exit Function
    End If

    Dim ws As Worksheet
    Dim col As Integer

    Set ws = target.Worksheet

    ' Column Names
    For col = 0 To rst.Fields.Count - 1
        ws.Cells(target.row, target.Column + col).Value = rst.Fields(col).Name
    Next
    ws.Range(ws.Cells(target.row, target.Column), _
        ws.Cells(target.row, target.Column + rst.Fields.Count)).Font.Bold = True

    ' Data from Recordset
    ws.Cells(target.row + 1, target.Column).CopyFromRecordset rst

    rst.Close
    con.Close

    Set rst = Nothing
    Set cmd = Nothing
    Set con = Nothing

    ImportSQLtoRange = 0

End Function

Code comments:

  • The query parameter can contain a SELECT or EXECUTE query.
  • The resulting data will be inserted starting from the top left cell of the target range.
  • Using Object types and the CreateObject function instead of direct use of ADO types

    lets to avoid setting ActiveX Data Objects 2.x Library references on user computers.

    This code works in Microsoft Excel 2003-2016.
  • Always use Set Nothing statements for ADODB.Connection and ADODB.Recordset objects to free resources.

Test Code of SQL Server Data Import to Excel using ADO

Sub TestImportUsingADO()

    Dim conString As String
    conString = GetTestConnectionString()

    Dim query As String
    query = GetTestQuery()

    Dim target As Range
    Set target = ThisWorkbook.Sheets(2).Cells(3, 2)

    target.CurrentRegion.Clear

    Select Case ImportSQLtoRange(conString, query, target)
        Case 1
            MsgBox "Import database data error", vbCritical
        Case Else
    End Select

End Sub

SQL Server Data Import to Excel using SaveToDB Add-In

The SaveToDB add-in allows connecting to databases, to text files, and the web using Data Connection Wizard, and supports OLE DB, ODBC, .NET and internal providers.

You can reload data using the Reload button at the ribbon or in the Context menu, or from VBA macros.

However, the add-in does not support connecting to new data sources from macros.

The add-in can save a lot of developer time when you need to implement changing query parameters.

You can modify the parameters by setting new values to named cells like Range(«Company») = «ABC».

You can learn about this feature in the attached SaveToDB examples for VBA developers.

Procedure TestImportUsingSaveToDB

The procedure reloads active table data.

The table is a native Excel ListObject connected using the Data Connection Wizard.

Sub TestImportUsingSaveToDB()

    Dim addIn As COMAddIn
    Dim addInObj As Object

    Set addIn = Application.COMAddIns("SaveToDB")
    Set addInObj = addIn.Object

    addInObj.Load

End Sub

Code comments:

If the table is an Excel ListObject connected to a database using OLE DB or ODBC, then the action is the same as ListObject.QueryTable.Refresh BackgroundQuery:=False.

In other cases (the web and file connections or databases through .NET providers), the add-in refreshes data using internal procedures. Moreover, the macro remains the same.

Excel Data Export to SQL Server using ADO

Function ExportRangeToSQL

The function exports the sourceRange data to a specified database table.

The optional beforeSQL code is executed before exporting, and the optional afterSQL code is executed after exporting.

The following logic of the export process is used in the example:

  1. Delete all data from a temporary import table.
  2. Export Excel data to the empty temporary import table.
  3. Update desired tables from the temporary import table data.

Specially developed stored procedures are used in the first and third steps.

You can adapt them to your task.

Moreover, a universal code is used to transfer Excel data to a destination table.

Function ExportRangeToSQL(sourceRange As Range, conString As String, table As String, _
    Optional beforeSQL = "", Optional afterSQL As String) As Integer

    On Error Resume Next

    ' Object type and CreateObject function are used instead of ADODB.Connection,
    ' ADODB.Command for late binding without reference to
    ' Microsoft ActiveX Data Objects 2.x Library
    ' ADO API Reference
    ' https://docs.microsoft.com/en-us/sql/ado/reference/ado-api/ado-api-reference?view=sql-server-ver16
    ' Dim con As ADODB.Connection
    Dim con As Object
    Set con = CreateObject("ADODB.Connection")

    con.ConnectionString = conString
    con.Open

    ' Dim cmd As ADODB.Command
    Dim cmd As Object
    Set cmd = CreateObject("ADODB.Command")

    ' BeginTrans, CommitTrans, and RollbackTrans Methods (ADO)
    ' http://msdn.microsoft.com/en-us/library/ms680895(v=vs.85).aspx

    Dim level As Long
    level = con.BeginTrans

    cmd.CommandType = 1             ' adCmdText
    If beforeSQL > "" Then
        cmd.CommandText = beforeSQL
        cmd.ActiveConnection = con
        cmd.Execute
    End If

    ' Dim rst As ADODB.Recordset
    Dim rst As Object
    Set rst = CreateObject("ADODB.Recordset")

    With rst
        Set .ActiveConnection = con
        .Source = "SELECT * FROM " & table
        .CursorLocation = 3         ' adUseClient
        .LockType = 4               ' adLockBatchOptimistic
        .CursorType = 0             ' adOpenForwardOnly
        .Open

        ' Column mappings

        Dim tableFields(100) As Integer
        Dim rangeFields(100) As Integer

        Dim exportFieldsCount As Integer
        exportFieldsCount = 0

        Dim col As Integer
        Dim index As Integer

        For col = 0 To .Fields.Count - 1
            index = 0
            index = Application.Match(.Fields(col).Name, sourceRange.Rows(1), 0)
            If index > 0 Then
                exportFieldsCount = exportFieldsCount + 1
                tableFields(exportFieldsCount) = col
                rangeFields(exportFieldsCount) = index
            End If
        Next

        If exportFieldsCount = 0 Then
            ExportRangeToSQL = 1
            Goto ConnectionEnd
        End If

        ' Fast read of Excel range values to an array
        ' for further fast work with the array

        Dim arr As Variant
        arr = sourceRange.Value

        ' The range data transfer to the Recordset

        Dim row As Long
        Dim rowCount As Long
        rowCount = UBound(arr, 1)

        Dim val As Variant

        For row = 2 To rowCount
            .AddNew
            For col = 1 To exportFieldsCount
                val = arr(row, rangeFields(col))
                If IsEmpty(val) Then
                Else
                    .Fields(tableFields(col)) = val
                End If
            Next
        Next

        .UpdateBatch
    End With

    rst.Close
    Set rst = Nothing

    If afterSQL > "" Then
        cmd.CommandText = afterSQL
        cmd.ActiveConnection = con
        cmd.Execute
    End If

    ExportRangeToSQL = 0

ConnectionEnd:

    con.CommitTrans

    con.Close
    Set cmd = Nothing
    Set con = Nothing

End Function

Code comments:

  • The preliminary column mappings are used for fast transferring Excel range column data to a Recordset column.
  • Excel data types are not verified.
  • Using Object types and the CreateObject function instead of direct use of ADO types

    lets to avoid setting ActiveX Data Objects 2.x Library references on user computers.

    This code works in Microsoft Excel 2003-2016.
  • Always use Set Nothing statements for ADODB.Connection and ADODB.Recordset objects to free resources.

Test Code of Excel Data Export to SQL Server

The temporary dbo04.ExcelTestImport table is used for inserting Excel data.

This table is cleared before exporting by the dbo04.uspImportExcel_Before stored procedure.

The dbo04.uspImportExcel_After stored procedure updates the source dbo04.ExcelTest table with values from dbo04.ExcelTestImport.

This technique simplifies the Excel part of an application but requires additional database objects and server side coding.

Sub TestExportUsingADO()

    Dim conString As String
    conString = GetTestConnectionString()

    Dim table As String
    table = "dbo04.ExcelTestImport"

    Dim beforeSQL As String
    Dim afterSQL As String

    beforeSQL = "EXEC dbo04.uspImportExcel_Before"
    afterSQL = "EXEC dbo04.uspImportExcel_After"

    Dim ws As Worksheet
    Set ws = ThisWorkbook.ActiveSheet

    Dim qt As QueryTable
    Set qt = GetTopQueryTable(ws)

    Dim sourceRange As Range

    If Not qt Is Nothing Then
        Set sourceRange = qt.ResultRange
    Else
        Set sourceRange = ws.Cells(3, 2).CurrentRegion
    End If

    Select Case ExportRangeToSQL(sourceRange, conString, table, beforeSQL, afterSQL)
        Case 1
            MsgBox "The source range does not contain required headers", vbCritical
        Case Else
    End Select

    ' Refresh the data
    If Not qt Is Nothing Then
        Call RefreshWorksheetQueryTables(ws)
    ElseIf ws.Name = ws.Parent.Worksheets(1).Name Then
    Else
        Call TestImportUsingADO
    End If

End Sub

The called RefreshWorksheetQueryTables procedure updates all worksheet QueryTables and ListObjects.

Sub RefreshWorksheetQueryTables(ws As Worksheet)

    On Error Resume Next

    Dim qt As QueryTable

    For Each qt In ws.QueryTables
        qt.Refresh BackgroundQuery:=True
    Next

    Dim lo As ListObject

    For Each lo In ws.ListObjects
        lo.QueryTable.Refresh BackgroundQuery:=True
    Next

End Sub

The called GetTopQueryTable function returns the most top QueryTable object connected to a database.

Function GetTopQueryTable(ws As Worksheet) As QueryTable

    On Error Resume Next

    Set GetTopQueryTable = Nothing

    Dim lastRow As Long
    lastRow = 0

    Dim qt As QueryTable
    For Each qt In ws.QueryTables
        If qt.ResultRange.row > lastRow Then
            lastRow = qt.ResultRange.row
            Set GetTopQueryTable = qt
        End If
    Next

    Dim lo As ListObject

    For Each lo In ws.ListObjects
        If lo.SourceType = xlSrcQuery Then
            If lo.QueryTable.ResultRange.row > lastRow Then
                lastRow = lo.QueryTable.ResultRange.row
                Set GetTopQueryTable = lo.QueryTable
            End If
        End If
    Next

End Function

Excel Data Export to SQL Server using SaveToDB Add-In

The SaveToDB add-in allows saving data changes from Excel to databases.

You can save data using the Save button at the ribbon or from VBA macros.

The simplest scenario is saving changes to a single target table. It works without coding.

Moreover, you can load data from tables, views, or stored procedures.

If you need to save the data to multiple normalized tables, you have to use stored procedures
for INSERT, UPDATE, and DELETE operations. It is not so hard.

Procedure TestExportUsingSaveToDB

The macro saves data changes of the active table to a database and reloads the data.

Sub TestExportUsingSaveToDB()

    Dim addIn As COMAddIn
    Dim addInObj As Object

    Set addIn = Application.COMAddIns("SaveToDB")
    Set addInObj = addIn.Object

    addInObj.Save

End Sub

Code comments:

The SaveToDB add-in makes a lot of work behind the scene.

It saves table metadata, a copy of loaded data, and data changes on hidden sheets.

You can even close the workbook. When the Save action is called, it builds and sends

INSERT, UPDATE and DELETE statements (or specified stored procedures) to a database.

You can learn hidden sheets using the SaveToDB, Options, Developer Options tab,
and generated SQL commands using the SaveToDB, Save, View Save Changes SQL menu item.

Connection String Functions

The example contains several useful functions for working with connection strings.

Function OleDbConnectionString

If the Username parameter is empty, the function returns an OLE DB connection string for trusted connection.

Function OleDbConnectionString(Server As String, Database As String, _
    Username As String, Password As String) As String

    If Username = "" Then
        OleDbConnectionString = "Provider=SQLOLEDB.1;Data Source=" & Server _
            & ";Initial Catalog=" & Database _
            & ";Integrated Security=SSPI;Persist Security Info=False;"
    Else
        OleDbConnectionString = "Provider=SQLOLEDB.1;Data Source=" & Server _
            & ";Initial Catalog=" & Database _
            & ";User ID=" & Username & ";Password=" & Password & ";"
    End If

End Function

Function OdbcConnectionString

If the Username parameter is empty, the function returns an ODBC connection string for trusted connection.

Function OdbcConnectionString(Server As String, Database As String, _
    Username As String, Password As String) As String

    If Username = "" Then
        OdbcConnectionString = "Driver={SQL Server};Server=" & Server _
            & ";Trusted_Connection=Yes;Database=" & Database
    Else
        OdbcConnectionString = "Driver={SQL Server};Server=" & Server _
            & ";UID=" & Username & ";PWD=" & Password & ";Database=" & Database
    End If

End Function

Conclusion

You can use the attached example code to import-export data between Microsoft Excel and SQL Server.

The code works fine with Microsoft SQL Server 2005-2016 and Microsoft Azure SQL Database, and in Microsoft Excel 2003-2016.

You can adapt it to another database platforms like MySQL, Oracle, or DB2 as the code uses OLE DB and ODBC connections.

You can also use the SaveToDB add-in as a database layer starting Excel 2007.

SaveToDB allows implementing projects with fewer efforts as it solves database layer tasks from the box.

See Also

  • Microsoft Office Development
  • ADO API Reference
  • How to import data from Microsoft SQL Server into Microsoft Excel
  • Using SaveToDB Add-In as VBA Library

Download

Понравилась статья? Поделить с друзьями:
  • Exporting to excel from mysql
  • Export from excel to sharepoint
  • Export from excel to postgresql
  • Export from excel to json
  • Exporting to excel from asp net