T-SQL OPENROWSET with Excel 仅当 运行 来自 SQL 服务器以外的机器时出错

T-SQL OPENROWSET with Excel Error only when run from machine other than the SQL server

所以这是我的问题。

我正在执行以下查询。第一个是我真正需要的,因为我想访问网络位置上的文件。第二个显示有希望帮助缩小问题范围。

我得到的错误是

Msg 7399, Level 16, State 1, Line 3
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 3
Cannot initialize the data source object of OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)".

注意:在下面的示例中,我用 myXXXX 替换了真实的文件、服务器和文件夹名称。但是,名称中没有任何内容会导致任何问题(没有空格、特殊字符等)

1. Select * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=\myFileServer\MyFolder\myFileName.xlsx;HDR=YES', 'SELECT * FROM [mySheetName$]')
2. Select * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0;Database=C:\MyFolder\myFileName.xlsx;HDR=YES', 'SELECT * FROM [mySheetName$]')

首先,了解一些背景信息。对于查询 2,我将 myFileName.xlsx 复制到托管 SQL Server 2008 实例的 Windows 服务器的 C: 驱动器。

Query 2: Accessing the file that is stored locally on the SQL Server box:
a. Run from SSMS connected to MySQLServer on SQl Server Box    directly: Works
b. Run from SSMS connected to MySQLServer on my    Windows 10 PC: Works 

无论 SSMS 是 运行 作为我的域管理员帐户的域用户帐户,以上两者都有效

Query 1:
c. Run from SSMS connected to MySQLServer on SQl Server Box directly: Works
d. Run from SSMS connected to MySQLServer on my Windows 10 PC: FAILS

甚至登录到 Windows 服务器和 运行 宁 SSMS 作为我的本地帐户工作。

下一步是检查 SQL 主机盒上的事件(顺便说一句:SQL 实例直接在主机 OS 上 运行ning,没有VM 涉及任何地方) 我在 SQL 主机箱上 运行 procmon,每次 运行 场景 d 时发现 4 个错误。以上。

Process = sqlservr.exe
Operation = CreateFile  
Path = \myFileServer\MyFolder\myFileName.xlsx
Result = ACCESS DENIED  
Details = Desired Access: Read Attributes, Disposition: Open, Options: Open For Backup, Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a, Impersonating: myDomainName\administrator

正在检查日志 运行ning 场景 c。以上给出

Process = sqlservr.exe
Operation = CreateFile  
Path = \myFileServer\MyFolder\myFileName.xlsx
Result = SUCCESS
Details = Desired Access: Read Attributes, Disposition: Open, Options: Open For Backup, Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a, Impersonating: myDomainName\administrator, OpenResult: Opened

我已检查文件夹和文件是否具有来自域管理员和域用户帐户以及 SQL 实例 运行 所在的服务帐户的完全访问权限。 我还检查了相同的帐户是否可以访问 SQL 主机箱上的 C:\temp。

我不太确定接下来要尝试什么。多年来,我一直在同一台服务器上导入这样的数据,而且我很确定我过去已经设法让它正常工作,但我发现的所有现有脚本都指向本地驱动器。

还有一件事:我也尝试将驱动器映射到文件夹,但遇到了同样的错误。

我希望有人能够为我指明正确的方向。

感谢阅读!

当我尝试访问的传播sheet 打开时(我自己或其他人),我得到了完全相同的错误。另外,sheet 只能在内存中打开,有时很难发现:

Msg 7399, Level 16, State 1, Line 54
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 54
Cannot initialize the data source object of OLE DB provider     "Microsoft.ACE.OLEDB.12.0" for linked server "(null)".

备注部分,在OPENROWSET documentation中,强调了关于远程OLEDB的几点:

OPENROWSET can be used to access remote data from OLE DB data sources only when the DisallowAdhocAccess registry option is explicitly set to 0 for the specified provider, and the Ad Hoc Distributed Queries advanced configuration option is enabled. When these options are not set, the default behaviour does not allow for ad hoc access.

When accessing remote OLE DB data sources, the login identity of trusted connections is not automatically delegated from the server on which the client is connected to the server that is being queried. Authentication delegation must be configured.

怀疑非本地文件违反了此限制。

我已将目标数据的 post 标记为答案,因为它为我指明了正确的方向。

除了添加指向 excel 文件的链接服务器之外,我找不到通过临时查询应用身份验证委托的方法,但对于我的要求,以下方法有效。

首先,我创建了一个新的 SQL 登录名 'ACEDriverLogin' 并为其分配了完全权限(添加了所有角色)。我想这不是必需的,所以我将开始删除角色,直到它中断,但现在必须这样做。

然后我将我的原始查询放入一个变量中,并使用新登录的凭据执行,如下所示。

DECLARE @cmdSQL nvarchar(max)
set @cmdSQL = 'Select * FROM OPENROWSET(''Microsoft.ACE.OLEDB.12.0'', ''Excel 12.0;Database=\myFileServer\MyFolder\myFileName.xlsx;HDR=YES'', ''SELECT * FROM [mySheetName$]'')'
exec(@cmdSQL) AS Login = 'ACEDriverLogin'