Friday, May 24, 2013

Read excel file from 64 bit SQL Server 2008 and 64 bit MS Office

When I tried to read excel file from SSMS like:


SELECT * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=D:\test.xlsx','SELECT * FROM [Sheet1$]')

I got following error: 

"SQL Server blocked access to STATEMENT 'OpenRowset/OpenDatasource' of component 'Ad Hoc Distributed Queries' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Ad Hoc Distributed Queries' by using sp_configure. For more information about enabling 'Ad Hoc Distributed Queries', see "Surface Area Configuration" in SQL Server Books Online."

To solve this error I ran these queries

sp_configure 'show advanced options', 1
GO
RECONFIGURE WITH OverRide
GO
sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE WITH OverRide
GO


Previous error has been solved but new error take birth:

"OLE DB provider 'Microsoft.Jet.OLEDB.4.0' cannot be used for distributed queries because the provider is configured to run in single-threaded apartment mode." 

To solve this I have downloaded  64bit version (AccessDatabaseEngine_X64.exe) of our "Microsoft Access Database Engine 2010 Redistributable" from the URL below :

http://www.microsoft.com/en-us/download/details.aspx?id=13255

I have installed AccessDatabaseEngine_X64.exe into my machine and run following command on SSMS.  

SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=D:\test.xls', 'SELECT * FROM [Sheet1$]')

Error change again:

"The OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)" reported an error. The provider did not give any information about the error.

   AND

"Cannot initialize the data source object of OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)"."

Now I have changed .xls to .xlsx WOW this time I got result. I need to do some more research why xls not worked but for now all is well.

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

All steps I have done not one by one, this is summarize things, behind this there is lot of goggling.