Cách import sql

In this article

Applies to:

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

There are several ways khổng lồ import data from Excel files to lớn SQL Server or to Azure Squốc lộ 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 and provides liên kết for more detailed information.

Bạn đang xem: Cách import sql

List of methods

You can use the following tools khổng lồ import data from Excel:

Export to text first (Squốc lộ Server and Squốc lộ Database)Directly from Excel (SQL Server on-premises only)
Import Flat File WizardSquốc lộ Server Import and Export Wizard
BULK INSERT statementSQL 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 khổng lồ run any of these tools once for each sheet.

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


Important

For detailed info about connecting to Excel files, & about limitations & known issues for loading data from or khổng lồ Excel files, see Load data from or khổng lồ Excel with SQL Server Integration Services (SSIS).


If you don"t have sầu SQL Server installed, or you have 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 SQL Server Import và Export Wizard. Optionally, save the settings as a Squốc lộ Server Integration Services (SSIS) package that you can customize và reuse later.

In Squốc lộ Server Management Studio, connect khổng lồ an instance of the SQL Server Database Engine.

Exp& Databases.

Right-click a database.

Point khổng lồ Tasks.

Clichồng one of the following options.

Import Data

Export Data

*

*

For an example of using the wizard lớn import from Excel to Squốc lộ Server, see Get started with this simple example of the Import & Export Wizard.

To learn about other ways khổng lồ launch the Import and Export wizard, see Start the SQL Server Import & Export Wizard.

SQL Server Integration Services (SSIS)

If you"re familiar with SSIS and don"t want lớn run the SQL Server Import and 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 build SSIS packages, see the tutorial How to Create an ETL Package.

*

OPENROWSET và linked servers


Important

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


Note

The ACE provider (formerly the Jet provider) that connects khổng lồ 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 inkhổng lồ SQL 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 test (CSV) file. 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 into a new database table.

USE ImportFromExcel;GOSELECT * INTO Data_dqFROM OPENROWSET("mbachulski.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("mbachulski.com.ACE.OLEDB.12.0", "Data Source=C:TempData.xlsx;Extended Properties=Excel 12.0")...;GOTo append the imported data to lớn 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 SQL Server to lớn the Excel file as a linked server. The following example imports the data from the Data worksheet on the existing Excel linked VPS EXCELLINK into lớn a new SQL Server database table named Data_ls.

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

Xem thêm: Bột Kiều Mạch Mua Ở Đâu Hà Nội, Bột Kiều Mạch

;GOYou can create a linked hệ thống 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
srvsản phẩm 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
hệ thống = "EXCELLINK"SET
srvhàng hóa = "Excel"SET
provider = "mbachulski.com.ACE.OLEDB.12.0"SET
datasrc = "C:TempData.xlsx"SET
provstr = "Excel 12.0"EXEC
RC = ..
hệ thống,
srvproduct,
provider,
datasrc,
location,
provstr,
catalogFor more info about linked servers, see the following topics:

For more examples and info about both linked servers & 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 BCPhường. tool, or Azure Data Factory - first you have lớn export your Excel data khổng lồ a text tệp tin.

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

If you want lớn export multiple worksheets from the workbook, select each sheet and then repeat this procedure. The Save sầu as comm& exports only the active 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, 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 sầu lớn 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 lớn SQL Wizard.

BULK INSERT command

BULK INSERT is a Transact-SQL commvà that you can run from SQL Server Management Studio. The following example loads the data from the Data.csv comma-delimited file inlớn an existing database table.

As described previously in the Prerequisite section, you have to lớn export your Excel data as text before you can use BULK INSERT khổng lồ import it. BULK INSERT can"t read Excel files directly. With the BULK INSERT commvà, you can import a CSV file 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 và examples for SQL Server and SQL Database, see the following topics:

BCP tool

BCPhường. is a program that you run from the commvà prompt. The following example loads the data from the Data.csv comma-delimited tệp tin inkhổng lồ the existing Data_bcp database table.

As described previously in the Prerequisite section, you have sầu khổng lồ export your Excel data as text before you can use BCPhường to lớn import it. BCPhường can"t read Excel files directly. Use lớn import into SQL Server or Squốc lộ Database from a test (CSV) file saved lớn 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 cộ 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 sầu lớn 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 topics:

Azure Data Factory

If you"re familiar with Azure Data Factory and don"t want to lớn run the Copy Wizard, create a pipeline with a Copy activity that copies from the text file khổng lồ SQL Server or to Azure Squốc lộ Database.

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 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 to copy data with Azure data factory, see the following topics:

Common errors

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

This error occurs because the OLEDB provider is not installed. Install it from mbachulski.com Access Database Engine 2010 Redistributable. Be sure khổng lồ install the 64-bit version if Windows và Squốc lộ Server are both 64-bit.

The full error is:

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

Cannot create an instance of OLE DB provider "mbachulski.com.ACE.OLEDB.12.0" for linked hệ thống "(null)"

This indicates that the mbachulski.com OLEDB has not been configured properly. Run the following Transact-Squốc lộ code to lớn resolve this:

EXEC sp_MSset_oledb_prop N"mbachulski.com.ACE.OLEDB.12.0", N"AllowInProcess", 1EXEC sp_MSset_oledb_prop N"mbachulski.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 "mbachulski.com.ACE.OLEDB.12.0" for linked VPS "(null)".

The 32-bit OLE DB provider "mbachulski.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 Squốc lộ 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 "mbachulski.com.ACE.OLEDB.12.0" cannot be loaded in-process on a 64-bit SQL Server.

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

Cannot initialize the data source object of OLE DB provider "mbachulski.com.ACE.OLEDB.12.0" for linked VPS "(null)"

Both of these errors typically indicate a permissions issue between the Squốc lộ Server process & the tệp tin. Ensure that the trương mục that is running the SQL Server service has full access permission lớn the file. We recommkết thúc against trying to import files from the desktop.

Xem thêm: Groovemonitor Là Gì - Groovemonitor Utility Là Gì

The full errors are:

Msg 7399, Level 16, State 1, Line 3The OLE DB provider "mbachulski.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.Msg 7303, Level 16, State 1, Line 3Cannot initialize the data source object of OLE DB provider "mbachulski.com.ACE.OLEDB.12.0" for linked VPS "(null)".

See Also

Import data from Excel or export data khổng lồ Excel with SQL Server Integration Services (SSIS)


Chuyên mục: Kiến thức