Tip of the Day : How to Join with an Inline User-Defined Function

SQL Server Helper - Tip of the Day

How to Read and Load an Excel 2007 or Excel 2010 File Without Using Import/Export Utility

To read an Excel 2007 or Excel 2010 file using OPENROWSET, run the following query:

SELECT * 
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database=C:\YourFile.xlsx;',
'SELECT * FROM [Sheet1$]')

If the Excel 2007 or Excel 2010 file does not contain any headers, simply add the “HDR=No” to the OPENROWSET command as follows:

SELECT * 
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database=C:\YourFile.xlsx;HDR=No',
'SELECT * FROM [Sheet1$]')

If one of the columns in the Excel 2007 or Excel 2010 file contains a mixed type of data, where some rows may contain just numbers while others may contain alphanumeric values, add the “IMEX=1” to the second parameter of the OPENROWSET command, as follows:

SELECT * 
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database=C:\YourFile.xlsx;IMEX=1',
'SELECT * FROM [Sheet1$]')

To load the Excel 2007 or Excel 2010 data to a new SQL Server table, simply add the INTO clause of the SELECT statement as follows:

SELECT * 
INTO [dbo].[MyExcelData]
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database=C:\YourFile.xlsx;',
'SELECT * FROM [Sheet1$]')

Back to Tip of the Day List Next Tip