从SQL Server中查询ORACLE中的数据,可以在SQL Server中创建到ORACLE的链接服务器来实现的,但是根据32位 、64位的机器和软件,需要用不同的驱动程序来实现。
在64位的机器上,通过访问接口:OracleProvide for OLE DB,来实现。
1、机器环境和软件环境
操作系统是:windows 7旗舰版 64位,SQL Server 20008R2 64 位,Oracle 11g 11.2.0.1.0 64 位。
2、ORACLE环境的设置
连接oracle,修改scott用户的密码为tiger,并给账户解锁,尝试登录,查询表:
C:UsersAdministrator>sqlplus / as sysdba SQL*Plus: Release 11.2.0.1.0 Production on 星期四 3月 13 16:11:03 2014 Copyright (c) 1982, 2010, Oracle. All rights reserved. 连接到: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing options SQL> alter user scott identified by tiger; 用户已更改。 SQL> alter user scott account unlock; 用户已更改。 SQL> connect scott/tiger 已连接。 SQL> select count(*) from emp; COUNT(*) ---------- 12
查看listener.ora是否正确:
# listener.ora Network Configuration File: D:appAdministratorproduct11.2.0dbhome_1 etworkadminlistener.ora # Generated by Oracle configuration tools. SID_LIST_LISTENER = (SID_LIST = (SID_DESC = (SID_NAME = CLRExtProc) (ORACLE_HOME = D:appAdministratorproduct11.2.0dbhome_1) (PROGRAM = extproc) (ENVS = "EXTPROC_DLLS=ONLY:D:appAdministratorproduct11.2.0dbhome_1inoraclr11.dll") ) (SID_DESC = (GLOBAL_DBNAME=orcl) (ORACLE_HOME = D:appAdministratorproduct11.2.0dbhome_1) (SID_NAME =orcl) ) ) LISTENER = (DESCRIPTION_LIST = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) ) )
要特别注意里面的:
(SID_DESC =
(GLOBAL_DBNAME=orcl)
(ORACLE_HOME = D:appAdministratorproduct11.2.0dbhome_1)
(SID_NAME =orcl)
)
另外,查看tnsnames.ora是否正确:
# tnsnames.ora Network Configuration File: D:appAdministratorproduct11.2.0dbhome_1NETWORKADMIN nsnames.ora # Generated by Oracle configuration tools. ORACLR_CONNECTION_DATA = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521)) ) (CONNECT_DATA = (SID = CLRExtProc) (PRESENTATION = RO) ) ) LISTENER_orcl = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) orcl = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) (CONNECT_DATA = (SID = orcl) (SERVER = DEDICATED) (SERVICE_NAME = orcl) ) )
特别是:
orcl =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
(CONNECT_DATA =
(SID = orcl)
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
查看监听器的状态是否正常:
SQL> host lsnrctl status LSNRCTL for 64-bit Windows: Version 11.2.0.1.0 - Production on 13-3月 -2014 16:16:21 Copyright (c) 1991, 2010, Oracle. All rights reserved. 正在连接到 (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))) LISTENER 的 STATUS ------------------------ 别名 LISTENER 版本 TNSLSNR for 64-bit Windows: Version 11.2.0.1.0 - Production 启动日期 13-3月 -2014 13:17:27 正常运行时间 0 天 2 小时 58 分 58 秒 跟踪级别 off 安全性 ON: Local OS Authentication SNMP OFF 监听程序参数文件 D:appAdministratorproduct11.2.0dbhome_1 etworkadminlistener.ora 监听程序日志文件 d:appadministratordiag nslsnrggg-PClisteneralertlog.xml 监听端点概要... (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=127.0.0.1)(PORT=1521))) 服务摘要.. 服务 "CLRExtProc" 包含 1 个实例。 实例 "CLRExtProc", 状态 UNKNOWN, 包含此服务的 1 个处理程序... 服务 "orcl" 包含 1 个实例。 实例 "orcl", 状态 UNKNOWN, 包含此服务的 1 个处理程序... 命令执行成功
3、设置链接服务器。
需要特别注意的是,数据源orcl指的是:tnsnames.ora文件中的orcl服务名。
第一步,选择“新建链接服务器”
第二步,一定要选择 Oracle Provide for OLE DB
第三步,输入产品:oracle,数据源:orcl
第四步,输入oracle的用户名和密码,这个需要根据实际情况设置,输入完后,点确定就好。
4、测试查询
select * from openquery(xxx, 'SELECT * FROM emp')
返回结果:
5、用代码实现:
/****** Object: LinkedServer [ORCL_SCOTT] Script Date: 03/13/2014 16:25:21 ******/ EXEC master.dbo.sp_addlinkedserver @server = N'ORCL_SCOTT', @srvproduct=N'oracle', @provider=N'OraOLEDB.Oracle', @datasrc=N'orcl' GO /* For security reasons the linked server remote logins password is changed with ######## */ EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'ORCL_SCOTT', @useself=N'False', @locallogin=NULL, @rmtuser=N'scott', @rmtpassword='tiger' go select * from openquery(ORCL_SCOTT, 'SELECT * FROM scott.emp')
还可以用?表示绑定变量:
EXEC ( 'SELECT * FROM scott.emp') AT ORCL_SCOTT; GO EXEC ( 'SELECT * FROM scott.emp WHERE MGR = ?', 7902) AT ORCL_SCOTT; GO DECLARE @v INT; SET @v = 7902; EXEC ( 'SELECT * FROM scott.emp WHERE MGR = ?', @v) AT ORCL_SCOTT; GO
6、通过openrowset函数来实现,更简单。
select * from openrowset('OraOLEDB.Oracle','orcl';'scott';'tiger', 'select * from SCOTT.EMP')
返回结果: