Creating table from excel in sql

Is there an easy way to create a table in SQL Server (2005) from an Excel spreadsheet. I’m thinking maybe some tool?

Thanks in advance.

asked Oct 7, 2008 at 15:56

Alonso's user avatar

SQL Server 2008R2+

  1. Right click the database
  2. Tasks
  3. Import Data
  4. Excel as Source
  5. choose an XLXS, XLS, etc… Good to go :)

UnDiUdin's user avatar

UnDiUdin

14.7k39 gold badges146 silver badges244 bronze badges

answered May 21, 2014 at 18:15

bird2920's user avatar

bird2920bird2920

3732 silver badges8 bronze badges

1

Save excel file as text(unicode) file and then use «sql import and export data» from all programs then select «flat file source» as file source and browse the txt file.Thats it.Simple and clear.

answered Mar 11, 2012 at 21:54

Abhinava Shukla's user avatar

1

If the data is not that big and if it is a simple table, easiest way is to create and open the table in SQL Server Management Studio and copy paste the excel data into it.

Other solutions are using DTS or using SSIS..

Community's user avatar

answered Oct 7, 2008 at 16:00

Gulzar Nazim's user avatar

Gulzar NazimGulzar Nazim

51.6k26 gold badges127 silver badges170 bronze badges

3

In your SQL Server enterprise manager there is an “import data” utility. It can take data in a variety of formats (.mdb, .XLS, .CSV, etc.). It is a simple job to point to the file that you want imported (into a new table) and start the upload of the data and structure. On the other hand the comments on how quirky Excel is accurate…. My suggestion is to cut and paste you data in an MS Acess table (All the data if it is a small table, about 10 records if it is very large). The MS Access data dump will identify any “quirk” with the data. Once the SQL table is created, uploading data into it via the MS Access table is just a matter of using the import manager (Source the MS Access table, Destination the SQL table)

answered Oct 13, 2008 at 20:32

Joe's user avatar

JoeJoe

7914 gold badges9 silver badges11 bronze badges

You can write VBA code in Excel:

  • Open a connection to the database
  • Build the table
  • Start filling records by looping through the rows of the spreadsheet Excel

answered Oct 7, 2008 at 16:02

You can treat the Excel document itself as a database using the builtin ODBC driver for that purpose. The fastest way is to open the ODBC Data Source Administrator, Configure the existing Excel Files data source and select a workbook. That gives you an Excel Database mapped to ODBC now all you need is a tool to read data from your ODBC database into your SQL Server database.

answered Oct 13, 2008 at 20:18

Rhubarb's user avatar

RhubarbRhubarb

34.3k2 gold badges47 silver badges35 bronze badges

How to Import an Excel Spreadsheet into a SQL Server Database Table Using the SQL Server Import and Export Wizard

Create a new SQL database table from an Excel spreadsheet in nine steps.

You can easily import a Microsoft SQL Server table from an Excel Spreadsheet by
using the SQL Server Import and Export Wizard. (You can also use the Import
and Export Wizard to
export data from a SQL Server table to an Excel spreadsheet.)

You can use the Wizard in the SQL Server Standard, Enterprise, Developer, or Evaluation
editions.

1. Enter the data into an Excel spreadsheet

  • First, enter the data into an Excel spreadsheet.
  • In this example, the default spreadsheet name, Sheet1, has been retained,
    but if you rename the sheet (to the name you want for the table in the database,
    for example), that name will be automatically used in the import process (at Step
    6).

Small demonstration Excel spreadsheet ready for transfer to a SQL database
Excel spreadsheet ready to export to a SQL table.

2. Start the SQL Import and Export Wizard

  • Next, in Windows, start the Import and Export Wizard at Start / All
    Programs / Microsoft SQL Server 2008/ Import and Export Data.
  • The Welcome page appears. Click Next.

SQL Import and Export Wizard in the Windows Start menu
Windows menu showing SQL Server Import and Export Wizard

Welcome page of the SQL Server Import and Export Wizard

3. Chose your Excel spreadsheet as the Data Source

  • In the Data Source dropdown of the Choose a Data Source page, select
    Microsoft Excel.
  • In the Excel file path box, specify the file path to the Excel spreadsheet.
  • Select the the version in the Excel version dropdown.
  • Make sure that First row has column names is checked.
  • Click Next.

Chose a Data Source page in the SQL Server Import and Export Wizard

4. Chose your SQL database as the destination

  • In the Destination dropdown list, accept the default setting of SQL Server
    Native Client 10.0
    .
  • In the Server name dropdown list, enter the name of the server. The example
    is a remote server, so the IP address and port of the server were specified.
  • Chose the Authentication type. The example is a remote server, so SQL Server
    authentication, with a user name and password, is required.
  • In the Database dropdown list, select or type the name of the database.
  • Click Next.

Chose a Data Destination page in the SQL Server Import and Export Wizard

5. Specify how to copy the data

  • The default option, Copy data from one or more tables or views, works for
    this example.
  • If you want to try the second option, Write a query to specify the data to transfer,
    the following lolcode snippet may be instructive:

CAN HAS SQL?

DBASE IZ GETDB(‘db/demo.db’)

FUNNAHS IZ DBUCKET(&DBASE&,»CAN I PLZ GET * ALL UP IN lollit»)

IM IN UR FUNNAHS ITZA TITLE

    VOTEZ IZ &TITLE#ups& — &TITLE#downs&

  • Click Next.

 Specify Table Copy or Query page in the SQL Server Import and Export Wizard

6. Select the source tables and views

  • The default settings as shown work in this example.
  • In the Destination column, you can specify a different name for the table
    in the SQL database if you choose.
  • Click Preview to see how your data will appear in the destination table.
  • You can click Edit Mappings to change how your data is assigned at the destination
    table, but it shouldn’t be necessary in this example since you entered the data
    into the Excel spreadsheet yourself.
  • Click Next.

Select Source Tables and Views page in the SQL Server Import and Export Wizard

7. Run the «Package»

The following message appears at the bottom of the Run Package page: In SQL
Server Express, Web, or Workgroup, you can run the package that the Import and Export
Wizard creates, but cannot save it. To save packages that the wizard creates, you
must upgrade to SQL Server Standard, Enterprise, Developer or Evaluation.

A «package» is all of the settings that your have configured so far. In
the commercial versions of SQL Server, you can save the package for reuse so you
don’t have to enter all of the settings the next time you run the wizard. In
the Express (free) version of Microsoft SQL Server 2008 Management Studio,
you must re-enter all of the settings every time you run the SQL Server Import and
Export wizard.

  • Click Next.

Run Package page in the SQL Server Import and Export Wizard

8. Verify that the package executed successfuly

  • Click Report to view useful information about the data transfer process.
  • Click Close.

The execution was successful page in the SQL Server Import and Export Wizard

9. View the new table in SQL Server Management Studio

  • View your new table by opening Microsoft SQL Server 2008 Management Studio
    at Start / All Programs / Microsoft SQL Server 2008/ Import and Export Data.

The new table, dbo.Sheet1$, in Microsoft SQL Server Management Studio
Successfully imported table in SQL Server Management Studio

You can also use the Import and Export Wizard to
export data from a table in SQL Server to an Excel spreadsheet.

Please send any suggestions or comments about this page to feedback at 66pacific.com.

The SQL Spreads Table Creator is a new feature in SQL Spreads 5.0 to create SQL Server tables directly from within Excel and pre-load them with the existing data in your Excel spreadsheet.

Table creator small.png

The Table Creator makes it possible to create SQL Server tables in just a few clicks. For those of you who are more advanced users, it is possible to fine tune the creation of the table like setting the exact data types on each column.

Prepare your data in an Excel

The SQL Spreads Table Creator uses the Excel table format to fetch extra information about the data formats, so you will need to have your data in an Excel table like this:

Tablecreator2.png

If you don’t have an Excel Table it is super easy to create one:

  1. Click a cell within your data range

  2. Press CTRL + T (hold down the CTRL-key and press T)

  3. The Create Table dialog will be shown:

Tablecreator3.png

  1. Click OK and your data will now be in an Excel table.

Create the table in SQL Server

  1. Click the Create SQL Table button in the SQL Spreads tab in Excel:
    image.png

  2. If this is the first time you are using SQL Spreads or the Table Creator, you will see the dialog to connect to your SQL Server.
    Table Creator Connect to Database

Fill out your server details and click OK

If you need help with connecting to your SQL Server, please see this article about how to connect to SQL Server.

If you have used the Table Creator before, the last used SQL Server connection will be used by default.

  1. Once you are connected to SQL Server, you will see the Create SQL Server Table dialog:

Create SQL Server Table Settings

Specify the table name and which SQL Server database to create the table in. SQL Spreads will always remember the last used table. If you need to change to another SQL Server click the Edit button next to the server name.

Fine tuning the data types to use in SQL Server

You will see all the table columns in Excel and be able to specify the exact data types to use. To use another data type, click the Type drop down list and select another data type for each required column:

Table Creator Data Types

For the following data types you can also specify the exact size of the column in the database:

  • Varchar and NVarchar: Can be set to the max number of characters or MAX to auto-adjust the size based on the content in the column.
  • Decimal and Numeric: The length must be entered as ‘(7,3)’ where the first number is the total number of figures and the last number is the number of figures after the decimal separator.

You can set a specific column to be the Primary Key by checking the Primary Key box.
Table Creator Primary Key Setting

If your columns should allow nulls, then check the Allow null box for each required column.
Table Creator Allow Nulls Setting

Default settings

The option Insert the rows from Excel into the new SQL Server table is enabled by default. If the setting is disabled, none of the rows from your raw data, that you used to create the table initially, will be inserted into the newly created table in the database.
Table Creator Settings Insert Rows

The option Add an auto-incrementing Identify column is enabled by default. This will automatically add an auto-incrementing Identity column into your table that you can name according to your requirements.
Table Creator Settings Identity Column

By default, the schema name dbo will be used for your newly created table. You can re-name the schema to align to your SQL Server table naming convention in your database by using the Use the schema name field.
Table Creator Settings Schema

  1. Click OK to create the table.

  2. A confirmation dialog will be show that the table was created and how many rows of data was inserted into the table:

Tablecreator7.png

Updating your new table using SQL Spreads

If you would like to update your newly created table from Excel, follow the steps below:

  1. Click the Design mode button twice to close and reopen the SQL Spreads Designer:
    image.png
  2. You should now see your new table in the Database tab in the SQL Spreads designer:
    image.png
  3. Select your table in the list to load it into Excel. You will now be able to update and manage the data in the table from within Excel.

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

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

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.
We recommend you run all tests in an isolated environment, at your own risk.



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

Понравилась статья? Поделить с друзьями:
  • Croc word ответы уровень
  • Creating symbols for word
  • Crm шаблон для excel
  • Creating sheets in excel
  • Crm таблица в excel шаблон