Hãy nâng cấp lên Microsoft Edge để tận dụng các tính năng mới nhất, bản cập nhật bảo mật và hỗ trợ kỹ thuật. Show
Import data from Excel to SQL Server or Azure SQL Database
Trong bài viết nàyApplies to: There are several ways to import data from Excel files to SQL Server 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 methodsThere 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 (SQL Server and SQL Database) Directly from Excel (SQL Server on-premises only) statement function If you want to import multiple worksheets from an Excel workbook, you typically have to run any of these tools once for each sheet. Import and Export WizardImport data directly from Excel files by using the SQL Server 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.
This launches the wizard: To learn more, review:
Integration Services (SSIS)If you're familiar with SQL Server Integration Services (SSIS) and don't want to 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. To learn more, review:
To start learning how to build SSIS packages, see the tutorial How to Create an ETL Package. OPENROWSET and linked serversNote 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 SQL Server, especially in automated processes or processes running in parallel, you may see unexpected results. Distributed queriesImport data directly into SQL Server from Excel files by using the Transact-SQL
3 or
4 function. This usage is called a distributed query. Before you can run a distributed query, you have to enable the
5 server configuration option, as shown in the following example. For more info, see ad hoc distributed queries Server Configuration Option.
The following code sample uses
3 to import the data from the Excel
7 worksheet into a new database table.
Here's the same example with
4.
To append the imported data to an existing table instead of creating a new table, use the
9 syntax instead of the
0 syntax used in the preceding examples. To query the Excel data without importing it, just use the standard
1 syntax. For more info about distributed queries, see the following articles:
Linked serversYou can also configure a persistent connection from SQL Server to the Excel file as a linked server. The following example imports the data from the
2 worksheet on the existing Excel linked server
3 into a new SQL Server database table named
4.
You can create a linked server from SQL Server Management Studio (SSMS), or by running the system stored procedure
5, as shown in the following example.
For more info about linked servers, see the following articles:
For more examples and info about both linked servers and distributed queries, see the following article:
Prerequisite - Save Excel data as textTo 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 WizardImport data saved as text files by stepping through the pages of the Import Flat File Wizard. As described previously in the 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
6 is a Transact-SQL command that you can run from SQL Server Management Studio. The following example loads the data from the
7 comma-delimited file into an existing database table. As described previously in the 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.
For more info and examples for SQL Server and SQL Database, see the following articles:
BCP toolBCP is a program that you run from the command prompt. The following example loads the data from the
7 comma-delimited file into the existing
9 database table. As described previously in the 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 SQL Server 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.
For more info about BCP, see the following articles:
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 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:
Azure Data FactoryIf 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 SQL Server or to Azure SQL Database. As described previously in the 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:
To start learning how to copy data with Azure data factory, see the following articles:
Common errorsMicrosoft.ACE.OLEDB.12.0" hasn't been registeredThis error occurs because the OLEDB provider isn't installed. Install it from Microsoft Access Database Engine 2016 Redistributable. Be sure to install the 64-bit version if Windows and SQL Server are both 64-bit. The full error is:
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:
The full error is:
The 32-bit OLE DB provider "Microsoft.ACE.OLEDB.12.0" cannot be loaded in-process on a 64-bit SQL ServerThis 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 and install the 64-bit version of the OLE DB provider instead. The full error is:
0 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 SQL Server process and the file. Ensure that the account that is running the SQL Server service has full access permission to the file. We recommend against trying to import files from the desktop. |