Q:如何用SQL语句查询Excel数据?
A:下列语句可在SQL SERVER中查询Excel工作表中的数据。
Excel 2007和2010版本:
SELECT * FROM OpenDataSource( 'Microsoft.ACE.OLEDB.12.0', 'Data Source="c:\book1.xlsx";User ID=Admin;
Password=;Extended properties=Excel 12.0')...[Sheet1$]
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0','Excel 12.0;Database=C:\book1.xlsx', 'SELECT * FROM [Sheet1$A1:D100]')
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0','Excel 12.0;Database=C:\book1.xlsx', 'SELECT * FROM [Sheet1$]')
Excel 2003版本:
SELECT * FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0','Data Source="c:\book1.xls";User ID=Admin;
Password=;Extended properties=Excel 8.0')...[Sheet1$]
SELECT * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=C:\book1.xls', 'SELECT * FROM [Sheet1$]')
SELECT * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=C:\book1.xls', 'SELECT * FROM [Sheet1$A1:D100]')
/*而且,还需要用拥有服务器sysadmin角色权限的登录帐号,先执行下列语句打开SQL SERVER的'Ad Hoc Distributed Queries'开关。*/
exec sp_configure 'show advanced options',1;reconfigure;
exec sp_configure 'Ad Hoc Distributed Queries',1;reconfigure;
/*执行查询后,再用相反顺序的语句,关闭'Ad Hoc Distributed Queries'开关。*/
exec sp_configure 'Ad Hoc Distributed Queries',0;reconfigure;
exec sp_configure 'show advanced options',0;reconfigure;