In this article

Applies to:

*
SQL Server (all supported versions)
*
Azure Squốc lộ Database

There are several ways to lớn import data from Excel files to Squốc lộ Server or lớn Azure SQL Database. Some methods let you import data in a single step directly from Excel files; other methods require you khổng lồ export your Excel data as text (CSV file) before you can import it. This article summarizes the frequently used methods & provides liên kết for more detailed information.

Bạn đang xem: Import data from excel to sql

List of methods

You can use the following tools to lớn import data from Excel:

Export to text first (SQL Server và SQL Database)Directly from Excel (Squốc lộ Server on-premises only)
Import Flat File WizardSQL Server Import và Export Wizard
BULK INSERT statementSquốc lộ Server Integration Services (SSIS)
BCPOPENROWSET function
Copy Wizard (Azure Data Factory)
Azure Data Factory
  

If you want to import multiple worksheets from an Excel workbook, you typically have sầu lớn run any of these tools once for each sheet.

A complete mô tả tìm kiếm of complex tools and services like SSIS or Azure Data Factory is beyond the scope of this menu. To learn more about the solution that interests you, follow the provided link.


Important

For detailed info about connecting to Excel files, & about limitations và known issues for loading data from or to lớn Excel files, see Load data from or to Excel with Squốc lộ Server Integration Services (SSIS).


If you don"t have Squốc lộ Server installed, or you have sầu SQL Server but don"t have Squốc lộ Server Management Studio installed, see Download SQL Server Management Studio (SSMS).

SQL Server Import & Export Wizard

Import data directly from Excel files by stepping through the pages of the Squốc lộ Server Import & Export Wizard. Optionally, save sầu the settings as a Squốc lộ Server Integration Services (SSIS) package that you can customize & reuse later.

In SQL Server Management Studio, connect to an instance of the Squốc lộ Server Database Engine.

Exp& Databases.

Right-click a database.

Point to lớn Tasks.

Clichồng one of the following options.

Import Data

Export Data

*

*

For an example of using the wizard to import from Excel to lớn SQL Server, see Get started with this simple example of the Import and Export Wizard.

To learn about other ways to launch the Import và Export wizard, see Start the SQL Server Import & Export Wizard.

Squốc lộ Server Integration Services (SSIS)

If you"re familiar with SSIS và don"t want lớn run the SQL Server Import & Export Wizard, create an SSIS package that uses the Excel Source and the SQL Server Destination in the data flow.

For more info about these SSIS components, see the following topics:

To start learning how to lớn build SSIS packages, see the tutorial How to Create an ETL Package.

*

OPENROWSET and linked servers


Important

In Azure SQL Database, you cannot import directly from Excel. You must first export the data to lớn a text (CSV) file. For examples, see Example.


Note

The ACE provider (formerly the Jet provider) that connects to lớn Excel data sources is intended for interactive sầu client-side use. If you use the ACE provider on Squốc lộ Server, especially in automated processes or processes running in parallel, you may see unexpected results.


Distributed queries

Import data directly inlớn Squốc lộ Server from Excel files by using the Transact-SQL OPENROWSET or OPENDATASOURCE function. This usage is called a distributed query.


Important

In Azure Squốc lộ Database, you cannot import directly from Excel. You must first export the data lớn a chạy thử (CSV) tệp tin. For examples, see Example.


Before you can run a distributed query, you have to lớn enable the ad hoc distributed queries VPS 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;GOsp_configure "Ad Hoc Distributed Queries", 1;RECONFIGURE;GOThe following code sample uses OPENROWSET to import the data from the Excel Sheet1 worksheet inlớn a new database table.

USE ImportFromExcel;GOSELECT * INTO Data_dqFROM OPENROWSET("gameviethot.com.ACE.OLEDB.12.0", "Excel 12.0; Database=C:TempData.xlsx", );GOHere"s the same example with OPENDATASOURCE.

USE ImportFromExcel;GOSELECT * INTO Data_dqFROM OPENDATASOURCE("gameviethot.com.ACE.OLEDB.12.0", "Data Source=C:TempData.xlsx;Extended Properties=Excel 12.0")...;GOTo append the imported data khổng lồ 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 topics:

Linked servers

You can also configure a persistent connection from Squốc lộ Server khổng lồ the Excel file as a linked server. The following example imports the data from the Data worksheet on the existing Excel linked VPS EXCELLINK inkhổng lồ a new SQL Server database table named Data_ls.

USE ImportFromExcel;GOSELECT * INTO Data_ls FROM EXCELLINK...

Xem thêm: Hướng Dẫn Sử Dụng 3D Album Để Tạo Album Ảnh Bằng 3D Album Ảnh Bằng 3D Album

;GOYou can create a linked VPS from Squốc lộ Server Management Studio, or by running the system stored procedure sp_addlinkedVPS, as shown in the following example.

DECLARE
RC intDECLARE
VPS nvarchar(128)DECLARE
srvhàng hóa nvarchar(128)DECLARE
provider nvarchar(128)DECLARE
datasrc nvarchar(4000)DECLARE
location nvarchar(4000)DECLARE
provstr nvarchar(4000)DECLARE
catalog nvarchar(128)-- Set parameter valuesSET
server = "EXCELLINK"SET
srvproduct = "Excel"SET
provider = "gameviethot.com.ACE.OLEDB.12.0"SET
datasrc = "C:TempData.xlsx"SET
provstr = "Excel 12.0"EXEC
RC = ..
VPS,
srvproduct,
provider,
datasrc,
location,
provstr,
catalogFor more info about linked servers, see the following topics:

For more examples và info about both linked servers and distributed queries, see the following topics:

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 sầu lớn export your Excel data lớn a text file.

In Excel, select File | Save sầu As and then select Text (Tab-delimited) (*.txt) or CSV (Comma-delimited) (*.csv) as the destination file type.

If you want to lớn export multiple worksheets from the workbook, select each sheet and then repeat this procedure. The Save as commvà exports only the active sầu sheet.


Tip

For best results with data importing tools, save sầu sheets that contain only the column headers và the rows of data. If the saved data contains page titles, blank lines, notes, & 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 khổng lồ import it.

For more info about the Import Flat File Wizard, see Import Flat File to lớn SQL Wizard.

BULK INSERT command

BULK INSERT is a Transact-Squốc lộ command that you can run from Squốc lộ Server Management Studio. The following example loads the data from the Data.csv comma-delimited file inkhổng lồ an existing database table.

As described previously in the Prerequisite section, you have lớn 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 commvà, you can import a CSV tệp tin that is stored locally or in Azure Blob storage.

USE ImportFromExcel;GOBULK INSERT Data_bi FROM "C:Tempdata.csv" WITH ( FIELDTERMINATOR = ",", ROWTERMINATOR = " ");GOFor more info and examples for SQL Server and SQL Database, see the following topics:

BCP tool

BCP.. is a program that you run from the comm& 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 lớn export your Excel data as text before you can use BCP khổng lồ import it. BCPhường can"t read Excel files directly. Use lớn import into SQL Server or SQL Database from a kiểm tra (CSV) tệp tin saved to local storage.


Important

For a text (CSV) tệp tin stored in Azure Blob storage, use BULK INSERT or OPENROWSET. For an examples, see Example.


bcp.exe pháo ImportFromExcel..Data_bcp in "C:Tempdata.csv" -T -c -t ,For more info about BCPhường, see the following topics:

Copy Wizard (Azure Data Factory)

Import data saved as text files by stepping through the pages of the Azure Data Factory Copy Wizard.

As described previously in the Prerequisite section, you have khổng lồ export your Excel data as text before you can use Azure Data Factory to lớn import it. Data Factory can"t read Excel files directly.

For more info about the Copy Wizard, see the following topics:

Azure Data Factory

If you"re familiar with Azure Data Factory and don"t want lớn run the Copy Wizard, create a pipeline with a Copy activity that copies from the text file to lớn SQL Server or khổng lồ Azure Squốc lộ 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 lớn import it. Data Factory can"t read Excel files directly.

For more info about using these Data Factory sources và sinks, see the following topics:

To start learning how lớn copy data with Azure data factory, see the following topics:

Comtháng errors

gameviethot.com.ACE.OLEDB.12.0" has not been registered

This error occurs because the OLEDB provider is not installed. Install it from gameviethot.com Access Database Engine 2010 Redistributable. Be sure to install the 64-bit version if Windows and Squốc lộ Server are both 64-bit.

The full error is:

Msg 7403, Level 16, State 1, Line 3The OLE DB provider "gameviethot.com.ACE.OLEDB.12.0" has not been registered.

Cannot create an instance of OLE DB provider "gameviethot.com.ACE.OLEDB.12.0" for linked server "(null)"

This indicates that the gameviethot.com OLEDB has not been configured properly. Run the following Transact-Squốc lộ code khổng lồ resolve this:

EXEC sp_MSset_oledb_prop N"gameviethot.com.ACE.OLEDB.12.0", N"AllowInProcess", 1EXEC sp_MSset_oledb_prop N"gameviethot.com.ACE.OLEDB.12.0", N"DynamicParameters", 1The full error is:

Msg 7302, Level 16, State 1, Line 3Cannot create an instance of OLE DB provider "gameviethot.com.ACE.OLEDB.12.0" for linked VPS "(null)".

The 32-bit OLE DB provider "gameviethot.com.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 SQL Server. To resolve this issue, uninstall the 32-bit version & install the 64-bit version of the OLE DB provider instead.

The full error is:

Msg 7438, Level 16, State 1, Line 3The 32-bit OLE DB provider "gameviethot.com.ACE.OLEDB.12.0" cannot be loaded in-process on a 64-bit Squốc lộ Server.

The OLE DB provider "gameviethot.com.ACE.OLEDB.12.0" for linked hệ thống "(null)" reported an error. The provider did not give sầu any information about the error

Cannot initialize the data source object of OLE DB provider "gameviethot.com.ACE.OLEDB.12.0" for linked hệ thống "(null)"

Both of these errors typically indicate a permissions issue between the Squốc lộ Server process và the file. Ensure that the tài khoản that is running the Squốc lộ Server service has full access permission to the tệp tin. We recommover against trying khổng lồ import files from the desktop.

The full errors are:

Msg 7399, Level 16, State 1, Line 3The OLE DB provider "gameviethot.com.ACE.OLEDB.12.0" for linked server "(null)" reported an error. The provider did not give sầu any information about the error.Msg 7303, Level 16, State 1, Line 3Cannot initialize the data source object of OLE DB provider "gameviethot.com.ACE.OLEDB.12.0" for linked VPS "(null)".

See Also

Import data from Excel or export data to Excel with Squốc lộ Server Integration Services (SSIS)