在 SQL Server 的在線幫助手冊中有這麼一段示例:
A. Use OPENROWSET with a SELECT and the Microsoft OLE DB Provider for SQL Server
This example uses the Microsoft OLE DB Provider for SQL Server to access the authors table in the pubs database on a remote server named seattle1. The provider is initialized from the datasource, user_id, and password, and a SELECT is used to define the row set returned.
USE pubs GO SELECT a.* FROM OPENROWSET('SQLOLEDB','seattle1';'sa';'MyPass', 'SELECT * FROM pubs.dbo.authors ORDER BY au_lname, au_fname') AS a GO
這個示例如果直接修改後放在 SQL Server 的 Enterprise Manager 中運行的話不會出錯,
但是如果把這段代碼的訪問數據方式用在 Query Analyzer 或者 使用Ole DB/ODBC 之類的組件執行的話,
會出現"Ad hoc access to OLE DB provider 'Microsoft.Jet.OLEDB.4.0' has been denied.
You must access this provider through a linked server" 的錯誤提示,也就是說,系統沒有打開OPENROWSET遠程訪問的許可,需要在目前 SQL Server 上建一個 "已連接Server",
添加的方法比較簡單,我的 SQL Server 是英文版,可以在 Enterprise Manager 中點開目前 SQL Server,
然對 Security -> Linked Servers 點右鍵,選 "新建...",參數可自行設定。
當然,這個方法隻能打開一個遠程數據服務連接,並且一直保持,如果要隨時可以打開任意遠程數據,可以用更改 OLE DB 和 MSDASQL 的遠程Ad hoc訪問開關設定值的方式更改,也就是更改注冊表,注冊表文件如下:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/MSSQLServer/Providers/SQLOLEDB]
"DisallowAdhocAccess"=dword:00000000
[HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/MSSQLServer/Providers/MSDASQL]
"DisallowAdHocAccess"=dword:00000000
但是需要注意的是,這樣就是變相地打開 SQL Server 的對外訪問安全控制。
參考 URL:
OPENROWSET "Ad hoc access to OLE DB provider 'Microsoft.Jet.OLEDB.4.0' has been denied.
http://www.microsoft.com/downloads/details.aspx?familyid=8f0a8df6-4a21-4b43
http://support.microsoft.com/default.aspx?scid=kb;en-us;327489
You must access this provider through a linked server"
關鍵字: