从SQL Server中查询ORACLE中的数据,可以在SQL Server中创建到ORACLE的链接服务器来实现的,但是根据32位 、64位的机器和软件,需要用不同的驱动程序来实现。
在64位的机器上,通过访问接口:OracleProvide for OLE DB,来实现。
1、测试环境
Microsoft Windows [版本 10.0.17134.556]
Microsoft SQL Server 2016 (SP2-GDR) (KB4293802) - 13.0.5081.1 (X64) (Build 17763: )
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
2、ORACLE环境的设置
Sqlserver服务器安装Oracle客户端
在Oracle上创建数据库用户并创建数据表
C:\Users\schina>sqlplus / as sysdba
SQL*Plus: Release 12.1.0.2.0 Production on 星期二 1月 22 14:40:41 2019
Copyright (c) 1982, 2014, Oracle. All rights reserved.
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options
SQL> drop user schina cascade;
SQL> create user schina identified by schina;
SQL> grant dba to schina;
SQL> conn schina/schina
SQL> create table testt(a int,b varchar2(100));
SQL> insert into testt values(123,'abc');
SQL> commit;
SQL>
配置listener.ora
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 123-111)(PORT = 1521))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
)
)
配置 tnsname.ora
ORCL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 123-111)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
配置Oracle网络安全,,
不配置此项可能报“ORA-12638: 身份证明检索失败” 错误
方法一 设置链接服务器
A.新建链接服务器
选择“新建链接服务器”
B.在弹出窗口的“常规”属性页,填写
链接服务器:链接别名,可随意
提供程序:OracleProviderfor OLE DB
产品名称:随意
数据源:为tnsname.ora内配置的名称
范围接口字符串:保留空即可
在“安全性”属性页,选择使用次安全上下文建立连接
远程登陆:oracle用户名
使用密码:oracle用户密码
C.可能错误
解决方法
在“访问接口”处,修改一下接口权限,
服务器对象》链接服务器》访问接口》OraOLEDB.Oracle
勾选“嵌套查询”和“允许进程内”
测试查询
select * from openquery(oracle18512, 'SELECT * FROM personinfo')
方法二 openrowset
也可以使用
select* fromopenrowset('OraOLEDB.Oracle', --接口
'orcl'; --tnsname配置名
'schina'; --用户名
'schina', --密码
'select * from testt') --语句
由于安全配置可能会报如下错误
SQLServer阻止了对组件“Ad Hoc Distributed Queries”的STATEMENT“OpenRowset/OpenDatasource”的访问,
因为此组件已作为此服务器安全配置的一部分而被关闭。
系统管理员可以通过使用sp_configure启用“Ad Hoc Distributed Queries”。
有关启用“Ad Hoc Distributed Queries”的详细信息,请搜索SQLServer联机丛书中的“Ad Hoc Distributed Queries”。
启用Ad Hoc Distributed Queries的方法,执行下面的查询语句就可以了:
execsp_configure'show advanced options',1
reconfigure
execsp_configure'Ad Hoc Distributed Queries',1
reconfigure