This article describes a method for querying Excel through SQL statements in Mssms.
Access to Excel is done primarily through the ACE data source provided by office, using OpenDataSource for access to Excel. That is, execute the following command in Mssms:
SELECT * from OpenDataSource (' microsoft.ace.oledb.12.0 ', ' data source= ' absolute path to Excel file "; Extended properties=excel 12.0 ') ... sheetname$--which ... and $ cannot be omitted
If the statement appears "SQL Server blocked access to component ' Ad Hoc distributed Queries ' STATEMENT ' Openrowset/opendatasource ', Because this component has been shut down as part of this server's security configuration. System administrators can enable ' Ad Hoc distributed Queries ' by using sp_configure. For more information about enabling ' Ad Hoc distributed Queries ', see "surface area Configurator" in SQL Server Books Online. "You can execute EXEC sp_configure ' show advanced options ', 1 reconfigure and exec sp_configure ' Ad Hoc distributed Queries before executing the statement ', 1 reconfigure, the previous sentence is open show advanced settings, and the latter is open ' Ad Hoc distributed Queries '. After executing these two articles, you will have no problem executing the statement that accesses Excel. If you execute these two settings statements, finally be sure to re-execute exec sp_configure ' Ad Hoc distributed Queries ', 0 reconfigure and exec sp_configure ' show advanced Options ', 0 reconfigure to turn these two settings off, because this setting is extremely unsafe for the database if set to open.
Access Excel---Select from excel in SQL Server