使用Microsoft.ACE.OLEDB.12.0将Excel导入SQL Server
尝试在SQL Server 2008 R2 R2 64位中打开Excel文件时出现以下错误:
Msg 7399, Level 16, State 1, Line 1 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. Msg 7303, Level 16, State 1, Line 1 Cannot initialize the data source object of OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)".
我正在使用以下查询:
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0; HDR=NO; IMEX=1; Database=\\filepath\filename.xlsx', 'SELECT * FROM [Sheet1$]')
有趣的是,DBA可以毫无问题地运行它。 我已经完成并运行以下查询:
sp_configure 'Show Advanced Options', 1; RECONFIGURE; GO sp_configure 'Ad Hoc Distributed Queries', 1; RECONFIGURE; GO EXEC sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'AllowInProcess', 1 GO EXEC sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'DynamicParameters', 1 GO
运行它的帐户看起来像有访问权限。 什么可能导致这个问题?
你有没有试过(作为一个testing)将Excel文件复制到SQL Server C:\驱动器并执行对该path的查询?
当您进入服务器并在资源pipe理器/运行对话框中打开此path时会发生什么情况: \ filepath \ filename.xlsx ?
你能够执行这个查询: exec master..xp_cmdshell'dir'\ filepath \ filename.xlsx' ?
这将帮助您确定是networking权利问题还是帐户有权使用分布式查询。
我的直觉是,这绝对是一个权限/许可问题,因为DBA可以运行它。
正如菲利普所说:首先检查xp_cmdshell的执行情况。 如果由于权限问题而没有运行,请首先通过运行重新configuration此选项
SP_CONFIGURE 'XP_CMDSHELL',1 GO RECONFIGURE
在运行以下命令之后,为ACE驱动程序的InProcessfunction启用链接服务器权限:
USE [master] GO EXEC master . dbo. sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0' , N'AllowInProcess' , 1 GO EXEC master . dbo. sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0' , N'DynamicParameters' , 1 GO
现在运行这一系列命令:
sp_configure 'show advanced options', 1 GO RECONFIGURE GO sp_configure 'Ad Hoc Distributed Queries', 1 GO RECONFIGURE
如果遇到错误,则分别运行每个命令。 最后运行下面的命令运行你所有的excel数据到SQL服务器:
SELECT * INTO TargetTableName FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=FilePath\fileName.xlsx;HDR=YES', 'SELECT * FROM [sheetName$]')
请记住,在xls的情况下,您必须使用Jet Driver而不是ACE。 在运行此查询之前,TargetTableName也不能存在。 快乐编码:)
SQL Server Management Studio。 在run命令中键入Services.msc以打开服务窗口。
searchSQL Server服务并右键单击它并select属性。
在“login”选项卡上,select系统帐户/或select您的域ID和帐户和密码。
一旦find您的login名称,按确定。
现在在这两个字段中input您的login密码。
重新启动服务,以便应用新的更改,如下图所示。
现在启动SQL Server Management Studio并尝试运行查询,如果仍然不能正常工作,请尝试重新启动系统。
或执行查询。
USE [master] GO EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0',N'AllowInProcess',1 GO EXEC master.dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0',N'DynamicParameters' ,1 GO