How to import Excel Data to ms SQL Server database

Source: Internet
Author: User
Tags dsn odbc connection knowledge base microsoft access database

In this example, use the following tools to import Excel Data:

SQL Server Data Transmission Service (DTS)
SQL Server connection Server
SQL Server Distributed Query
ActiveX Data Object (ADO) and Microsoft ole db provider for SQL Server
ADO and Microsoft ole db provider for Jet 4.0

Back to Top

Requirements


The following list lists the recommended hardware, software, network architecture, and required service packages:

Available instances of Microsoft SQL Server 7.0 or Microsoft SQL Server 2000
Microsoft Visual Basic 6.0 (for ADO examples using Visual Basic)

This section assumes that you are familiar with the following topics:

Data Transmission Service
Link server and Distributed Query
Ado development in Visual Basic

Back to Top

Example


Import and append


The example SQL statement used in this article demonstrates the "Create Table" query. This query imports Excel data into a new SQL Server table by using the Select... into... from syntax. As shown in the code example, when you continue to reference the source and target objects, you can use the insert into... select... from syntax to convert these statements into append queries.

Use DTS


You can use the SQL Server Data Transmission Service (DTS) Import Wizard to import Excel Data to the SQL Server table. When you step through the wizard and select an Excel source table, remember that the EXCEL object name with the dollar sign ($) appended represents the worksheet (for example, sheet1 $ ), the normal object name without the dollar sign represents the range specified by Excel.

Use linked servers


To simplify the query, you can configure the Excel Workbook as a link server in SQL Server. For other information, click the following article number to view the article in the Microsoft Knowledge Base:

306397 (http://support.microsoft.com/kb/306397/) how to combine SQL server linked servers and distributed queries using Excel

Run the following code to import the MERs worksheet data on the Excel link server "excellink" to a new SQL Server table named xlimport1:

SELECT * INTO XLImport1 FROM EXCELLINK...[Customers$]

You can also use openquery to query source data in the following way:

SELECT * INTO XLImport2 FROM OPENQUERY(EXCELLINK,    'SELECT * FROM [Customers$]')

Use Distributed Query


If you do not want to configure a persistent connection to an Excel Workbook as a linked server, you can use the OpenDataSource or OpenRowSet function to import data for a specific purpose. The following code example can also import the Excel MERs worksheet data to a new SQL Server table:

SELECT * INTO XLImport3 FROM OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0','Data Source=C:/test/xltest.xls;Extended Properties=Excel 8.0')...[Customers$]SELECT * INTO XLImport4 FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=C:/test/xltest.xls', [Customers$])SELECT * INTO XLImport5 FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=C:/test/xltest.xls', 'SELECT * FROM [Customers$]')

Use ADO and sqloledb


When Microsoft ole db for SQL Server (sqloledb) is used to connect to SQL server in the ADO application, you can use the same "Distributed Query" syntax as the "Use Distributed Query" section to import Excel Data to SQL Server.

The following Visual Basic 6.0 code example requires that you add a project reference to ActiveX Data Object (ADO. This sample code also demonstrates how to use OpenDataSource and OpenRowset on the sqloledb connection.

    Dim cn As ADODB.Connection    Dim strSQL As String    Dim lngRecsAff As Long    Set cn = New ADODB.Connection    cn.Open "Provider=SQLOLEDB;Data Source=<server>;" & _        "Initial Catalog=<database>;User ID=<user>;Password=<password>"    'Import by using OPENDATASOURCE.    strSQL = "SELECT * INTO XLImport6 FROM " & _        "OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0', " & _        "'Data Source=C:/test/xltest.xls;" & _        "Extended Properties=Excel 8.0')...[Customers$]"    Debug.Print strSQL    cn.Execute strSQL, lngRecsAff, adExecuteNoRecords    Debug.Print "Records affected: " & lngRecsAff    'Import by using OPENROWSET and object name.    strSQL = "SELECT * INTO XLImport7 FROM " & _        "OPENROWSET('Microsoft.Jet.OLEDB.4.0', " & _        "'Excel 8.0;Database=C:/test/xltest.xls', " & _        "[Customers$])"    Debug.Print strSQL    cn.Execute strSQL, lngRecsAff, adExecuteNoRecords    Debug.Print "Records affected: " & lngRecsAff    'Import by using OPENROWSET and SELECT query.    strSQL = "SELECT * INTO XLImport8 FROM " & _        "OPENROWSET('Microsoft.Jet.OLEDB.4.0', " & _        "'Excel 8.0;Database=C:/test/xltest.xls', " & _        "'SELECT * FROM [Customers$]')"    Debug.Print strSQL    cn.Execute strSQL, lngRecsAff, adExecuteNoRecords    Debug.Print "Records affected: " & lngRecsAff    cn.Close    Set cn = Nothing

Use ADO and jet provider


The example in the previous section uses ADO and sqloledb provider to connect to the target for importing data from Excel to SQL. You can also use ole db provider for Jet 4.0 to connect to the Excel source.

The jet data engine can reference external databases in SQL statements by using special syntaxes in three different formats:

[Full path to Microsoft Access database]. [Table Name]
[Isam name;Isam connection string]. [Table Name]
[ODBC;ODBC connection string]. [Table Name]

This section uses the third format to create an ODBC connection to the target SQL Server database. You can use the ODBC Data Source Name (DSN) or DSN-less to connect to the string:

DSN:    [odbc;DSN=<DSN name>;UID=<user>;PWD=<password>]DSN-less:   [odbc;Driver={SQL Server};Server=<server>;Database=<database>;       UID=<user>;PWD=<password>]

The following Visual Basic 6.0 code example requires that you add a project reference to ADO. This sample code demonstrates how to use the JET 4.0 provider to import Excel Data to SQL Server through an ADO connection.

    Dim cn As ADODB.Connection    Dim strSQL As String    Dim lngRecsAff As Long    Set cn = New ADODB.Connection    cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _        "Data Source=C:/test/xltestt.xls;" & _        "Extended Properties=Excel 8.0"        'Import by using Jet Provider.    strSQL = "SELECT * INTO [odbc;Driver={SQL Server};" & _        "Server=<server>;Database=<database>;" & _        "UID=<user>;PWD=<password>].XLImport9 " & _        "FROM [Customers$]"    Debug.Print strSQL    cn.Execute strSQL, lngRecsAff, adExecuteNoRecords    Debug.Print "Records affected: " & lngRecsAff            cn.Close    Set cn = Nothing

You can also use this syntax (supported by jet provider) to import Excel data to other Microsoft Access databases, index sequential access methods (isam) ("desktop") databases, or ODBC databases.

Back to Top

Troubleshooting


Remember that the EXCEL object name appended with the dollar sign ($) represents the worksheet (for example, sheet1 $), and the normal object name represents the range specified by Excel.
In some environments, especially when the table name replaces the SELECT query assignment Excel source data, the columns in the target SQL Server table are rearranged alphabetically. For other information about this issue in Jet provider, click the following article number to view the article in the Microsoft Knowledge Base:

299484 (http://support.microsoft.com/kb/299484/) PRB: Use ADOX to retrieve access table columns in alphabetical order
When jet provider determines that an Excel column contains mixed text and numeric data, jet provider selects the "majority" data type and returns unmatched values as null. For more information about how to solve this problem, click the following article number to view the article in the Microsoft Knowledge Base:

194124 (http://support.microsoft.com/kb/194124/) PRB: the Excel return value is null when Dao openrecordset is used

Back to Top

Reference

For more information about how to use Excel as a data source, click the following article number to view the article in the Microsoft Knowledge Base:

257819 (http://support.microsoft.com/kb/257819/) how to use ADO in Visual Basic or VBA to process Excel Data

For more information about how to transfer data to excel, click the following article number to view the article in the Microsoft Knowledge Base:

295646 (http://support.microsoft.com/kb/295646/) how to use ADO to transfer data from an ADO data source to an Excel 247412 (http://support.microsoft.com/kb/247412/) info: how to transfer data from Visual Basic to excel 246335 (http://support.microsoft.com/kb/246335) how to Use automation to transfer data from an ADO record set to excel 319951 (http://support.microsoft.com/kb/319951/) how to transfer data 306125 (http://support.microsoft.com/kb/306125/) to excel through SQL Server Data Transfer Service) how to import data from Microsoft SQL server to Microsoft Excel
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.