How To Import Data From Excel To SQL Server
How To Import Data From Excel To SQL Server
Article ID: 321686 - Last Review: December 14, 2005 - Revision: 3.3
How to import data from Excel to SQL Server
This article was previously published under Q321686
SUMMARY
This step-by-step article demonstrates how to import data from Microsoft Excel
worksheets into Microsoft SQL Server databases by using a variety of methods.
Requirements
The following list outlines the recommended hardware, software, network
infrastructure, and service packs that are required:
Available instance of Microsoft SQL Server 7.0 or Microsoft SQL Server 2000 or Microsoft SQL Server 2005
Microsoft Visual Basic 6.0 for the ADO samples that use Visual Basic
Portions of this article assume that you are familiar with the following topics:
Samples
Import vs. Append
The sample SQL statements that are used in this article demonstrate C reate Table
queries that import Excel data into a new SQL Server table by using the SELEC T...INTO...FROM syntax. You can convert
these statements to Append queries by using the INSERT INTO...SELEC T...FROM syntax while you continue to reference
the source and destination objects as shown in these code samples.
You can also execute the query against the source in a passthrough manner by using OPENQUERY as follows:
The following Visual Basic 6.0 code sample requires that you add a project reference to ActiveX Data Objects (ADO).
This code sample also demonstrates how to use OPENDATASOURC E and OPENROWSET over an 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
The Jet database engine can reference external databases in SQL statements by using a special syntax that has three
different formats:
This section uses the third format to make an ODBC connection to the destination SQL
Server database. You can use an ODBC Data Source Name (DSN) or a DSN-less connection string:
The following Visual Basic 6.0 code sample requires that you add a project reference to ADO. This code sample
http://support.microsoft.com/kb/321686 2/4
22-06-2010 How
The following Visual Basic 6.0 code sample to import
requires data
that youfrom
addExcel to SQLreference
a project S… to ADO. This code sample
demonstrates how to import Excel data to SQL Server over an ADO connection by using the Jet 4.0 Provider.
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, which the Jet Provider supports, to import Excel data into other Microsoft Access
databases, indexed sequential access method (ISAM) ("desktop") databases, or ODBC databases.
Troubleshooting
Remember that Excel object names that are appended with a dollar sign ($) represent worksheets (for example,
Sheet1$) and that plain object names represent Excel named ranges.
In some circumstances, especially when you designate the Excel source data by using the table name instead of a
SELEC T query, the columns in the destination SQL Server table are rearranged in alphabetical order.For
additional information about this problem with the Jet Provider, click the article number below to view the article
in the Microsoft Knowledge Base: 299484 (http://support.m icrosoft.com /kb/299484/EN-US/ ) PRB: C olumns
Are Sorted Alphabetically When You Use ADOX to Retrieve C olumns of Access Table
When the Jet Provider determines that an Excel column contains mixed text and numeric data, the Jet Provider
selects the "majority" data type and returns non-matching values as NULLs.For additional information about how
to work around this problem, click the article number below to view the article in the Microsoft Knowledge Base:
194124 (http://support.m icrosoft.com /k b/194124/EN-US/ ) PRB: Excel Values Returned as NULL Using DAO
OpenRecordset
REFERENC ES
For additional information about how to use Excel as a data source, click the article
number below to view the article in the Microsoft Knowledge Base: 257819
(http://support.m icrosoft.com /k b/257819/EN-US/ ) HOWTO: Use ADO with Excel Data from Visual Basic or VBA For
additional information about how to transfer data into Excel, click the article numbers below to view the articles in the
Microsoft Knowledge Base: 295646 (http://support.m icrosoft.com /kb/295646/EN-US/ ) HOWTO: Transfer Data from
ADO Data Source to Excel with ADO 247412 (http://support.m icrosoft.com /kb/247412/EN-US/ ) INFO: Methods for
Transferring Data to Excel from Visual Basic 246335 (http://support.m icrosoft.com /k b/246335/EN-US/ ) HOWTO:
Transfer Data from an ADO Recordset to Excel with Automation 319951
(http://support.m icrosoft.com /k b/319951/EN-US/ ) HOW TO: Transfer Data to Excel by Using SQL Server Data
Transformation Services 306125 (http://support.m icrosoft.com /kb/306125/EN-US/ ) HOW TO: Import Data from
SQL Server into Microsoft Excel
APPLIES TO
http://support.microsoft.com/kb/321686 3/4
22-06-2010 How to import data from Excel to SQL S…
Microsoft Support
©2010 Microsoft
http://support.microsoft.com/kb/321686 4/4