TSQL使用ADHOC访问Excle文件

时间:2023-03-09 03:35:44
TSQL使用ADHOC访问Excle文件

如题,今天正好碰到这个问题,现将相关知识点记录如下:

--开启高级配置功能
EXEC sp_configure 'show advanced options',1
reconfigure
--开启导入功能
EXEC sp_configure 'Ad Hoc Distributed Queries',1
reconfigure
--允许在进程中使用ACE.OLEDB.12
EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'AllowInProcess', 1
--允许使用动态参数
EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'DynamicParameters', 1
--使用ADHOC读取Excle文件
SELECT *
FROM OPENROWSET
('Microsoft.ACE.OLEDB.12.0' ,'Excel 12.0;HDR=YES
;DATABASE=d:\myexcel.xlsx' ,'SELECT * FROM [Sheet2$]')