SQL Server: Import data from Excel
Posted by cavemansblog on July 6, 2009
This article will demonstrate one of the several ways of importing data into a SQL Server table from an Excel spread sheet. I will use a simple SQL query to import data instead of SSIS/DTS. The precursor to achieving the data transfer will be to perform a onetime operation via, enabling the running of Ad Hoc Distributed Queries on your SQL Server database. This can be accomplished by using the SQL Server 2005 Surface Area Configuration Utility or by executing the following code in SQL Server Management Studio’s Query Editor/Query Analyzer:
sp_configure 'show advanced options', 1 GO reconfigure GO sp_configure 'Ad Hoc Distributed Queries', 1 GO reconfigure
When the server is capable of running Ad Hoc Distributed Queries we are all set to run SQL queries to import data from an MS-Excel spreadsheet. It must kept in mind that an Excel file with all the intended columns and data has to exit, before data can be imported (also the file must be closed when data is transferred). In the following code sample, data from the spreadsheet “EMP” in the Excel file, will be imported into the “employee” table in the SQL Server database using the OPENROWSET.
--Export data to Excel
select * into Employee from openrowset('Microsoft.Jet.OLEDB.4.0' ,
'Excel 8.0;Database=c:\test\test.xls','Select * from [EMP$]')
In the near future I will demonstrate how to import multiple spreadsheets into SQL Server database using SSIS, via dynamic discovery of all available spreadsheets in one Excel file.
References:
1. Server Settings Configuration Options
Possibly related posts: (automatically generated)
This entry was posted on July 6, 2009 at 10:07 am and is filed under Sql Server, Sudheer Reddy Battula. Tagged: Ad Hoc Distributed Query, import from excel, sql server 2000/2005. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

SQL Server – Import Data From Excel « experiment said
[...] References:http://cavemansblog.wordpress.com/2009/07/06/sql-server-import-data-from-excel/http://msdn.microsoft.com/en-us/library/ms189631.aspxhttp://support.microsoft.com/kb/321686 [...]
Tobias Persson said
Hi,
just wanted to add that I too love to use openrowset for importing data from excel spreadsheets, but it could be of value to mention that the information above is of no use if your SQL-server 2005 environment is of 64-bit. Openrowset is not supported and probably never will in SQL-server 2005/2008 64-bit.
Catherine Bell said
Absolutely UNBELIEVABLE that OPENROWSET is not supported in SQL2005/2008 64-bit….Did I say UNBELIEVABLE? INCREDULOUS!!!