I wrote a blog earlier about reading data from excel via SQL Server. This was done using ‘OpenRowset. One of my blog readers sent an email to me that they were getting an error. In this blog, we would see how to fix Msg 15281 – SQL Server blocked access to STATEMENT ‘OpenRowset/OpenDatasource’ of component ‘Ad Hoc Distributed Queries’
Here is the query which my blog reader was using.
SELECT * FROM OPENROWSET( 'Microsoft.ACE.OLEDB.12.0' ,'Excel 12.0;Database=\\\\FileServer\\ExcelShare\\HRMSDATA.xlsx;HDR=YES;IMEX=1' ,'SELECT * FROM [EMPMASTER$]')
the error message while running the query is as follows.
Msg 15281, Level 16, State 1, Line 1
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’, search for ‘Ad Hoc Distributed Queries’ in SQL Server Books Online.
Here is the T-SQL which can be used to enable the usage of open rowset.
EXEC sp_configure 'show advanced options', 1 RECONFIGURE WITH OVERRIDE GO EXEC sp_configure 'ad hoc distributed queries', 1 RECONFIGURE WITH OVERRIDE GO
After this, he was able to pull data from excel sheet to SQL Server. Well, this was quite a simple solution for a complicated looking problem. If you have alternative suggestions, please do share.
Reference: Pinal Dave (https://blog.sqlauthority.com)