在SQL Server中使用OPENROWSET訪問ORACLE數據庫時,你可能會遇到各類坑,下面一一梳理一下你會遇到的一些坑。 數據庫
1:數據庫沒有開啓"Ad Hoc Distributed Queries"選項,那麼你就會遇到下面坑。 服務器
SELECT TOP 10 * FROM OPENROWSET('OraOLEDB.Oracle', 'ESCM_134';'test';'test', 'SELECT * FROM TEST.MY_SET')
Msg 15281, Level 16, State 1, Line 1 app
SQL Server blocked access to STATEMENT 'OpenRowset/OpenDatasource' of component 'Ad Hoc Distributed Queries' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Ad Hoc Distributed Queries' by using sp_configure. For more information about enabling 'Ad Hoc Distributed Queries', search for 'Ad Hoc Distributed Queries' in SQL Server Books Online. ide
出現這個錯誤,只須要開啓數據庫"Ad Hoc Distributed Queries"選項便可。以下所示函數
sp_configure 'show advanced option',1;
GO
RECONFIGURE
sp_configure 'Ad Hoc Distributed Queries',1;
GO
RECONFIGURE
2:遇到「The OLE DB provider "OraOLEDB.Oracle" for linked server ....."這個坑 ui
SELECT TOP 10 * FROM OPENROWSET('OraOLEDB.Oracle', 'ESCM_134';'test';'test', 'SELECT * FROM TEST.MY_SET')
Msg 7399, Level 16, State 1, Line 1 this
The OLE DB provider "OraOLEDB.Oracle" for linked server "(null)" reported an error. Access denied. spa
Msg 7350, Level 16, State 2, Line 1 rest
Cannot get the column information from OLE DB provider "OraOLEDB.Oracle" for linked server "(null)". code
解決這個也比較簡單,使用SSMS鏈接到數據庫後,在「Server Objects」->"Linked Servers"->"OraOLEDB.Oracle"下勾選「Allow inprocess"選項。注意,若是不重啓,沒法使之生效,依然會報上面錯誤。
3:遇到「OLE DB provider "OraOLEDB.Oracle" for linked server "(null)" returned message "ORA-12154: TNS: 沒法解析指定的鏈接標識符...."這個坑
SELECT TOP 10 * FROM OPENROWSET('OraOLEDB.Oracle', 'ESCM_134';'test';'test', 'SELECT * FROM TEST.MY_SET')
OLE DB provider "OraOLEDB.Oracle" for linked server "(null)" returned message "ORA-12154: TNS: 沒法解析指定的鏈接標識符".
Msg 7303, Level 16, State 1, Line 1
Cannot initialize the data source object of OLE DB provider "OraOLEDB.Oracle" for linked server "(null)".
遇到這個問題有幾種情形:
1: 你沒有在TNS配置文件裏面配置相關ORACLE實例信息。
2: SQL Server數據庫是64位的,你只安裝了32bit數據庫,配置了Oracle Client 32bit下的TNS,或者Oracle Client 32/64位都安裝了,可是你只配置了32位下的TNS。其實只須要配置64下的TNS便可。由於64位的SQL Server確定調用64位的驅動程序。
4:普通帳號遇到「Ad hoc access to OLE DB provider 'OraOLEDB.Oracle' has been denied. You must access this provider through a linked server."錯誤, 具備sysadmin角色的帳號執行下面SQL正常,可是很是普通的帳號就一直報下面錯誤
SELECT TOP 10 * FROM OPENROWSET('OraOLEDB.Oracle', 'ESCM_134';'test';'test', 'SELECT * FROM TEST.MY_SET')
Msg 7415, Level 16, State 1, Line 1
Ad hoc access to OLE DB provider 'OraOLEDB.Oracle' has been denied. You must access this provider through a linked server.
解決方法,在服務器打開註冊表,在HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL12.MSSQLSERVER\Providers\OraOLEDB.Oracle下(固然不一樣版本或命名實例的數據庫,這個註冊表路徑有啥不一樣,根據實際狀況找到OraOLEDB.Oracle),新建DisallowAdHocAccess選項便可解決問題。
DisallowAdHocAccess屬性設置爲 1,SQL Server 不容許特別經過 OPENROWSET 和 OPENDATASOURCE 函數根據指定的 OLE DB 提供程序訪問。若是您嘗試調用這些函數中的特殊查詢,您會收到相似於如下內容的錯誤消息
Server: Msg 7415, Level 16, State 1, Line 1 Ad hoc access to OLE DB provider 'Microsoft.Jet.OLEDB.4.0' has been denied. You must access this provider through a linked server.
In other words, with the DisallowAdHocAccess property set to 1 for a specific OLE DB provider, you must use a predefined linked server setup for the specific OLE DB provider. You can no longer pass in an ad hoc connection string that references that provider to the OPENROWSET or the OPENDATASOURCE function.
參考資料: