从SQL Server中查询ORACLE中的数据,可以在SQL Server中创建到ORACLE的链接服务器来实现的,但是根据32位 、64位的机器和软件,需要用不同的驱动程序来实现。
在32位的机器上,通过访问接口:Microsoft OLE DB Provide for Oracle,来实现。
1、机器环境和软件环境
机器是一台虚拟机,安装了windows xp,SQL Server 20008R2,Oracle 10g 10.2.0.1.0。
2、ORACLE环境的设置
连接oracle:
C:\Documents and Settings\Administrator>sqlplus / as sysdba
SQL*Plus: Release 10.2.0.1.0 - Production on 星期四 3月 13 15:22:29 2014
Copyright (c) 1982, 2005, Oracle. All rights reserved.
连接到:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
修改scott用户的密码为tiger,并给账户解锁,尝试登录,查询表:
SQL> alter user scott identified by tiger;
用户已更改。
SQL> alter user scott account unlock;
用户已更改。
SQL> connect scott/tiger
已连接。
SQL> select count(*) from emp;
COUNT(*)
----------
14
查看listener.ora是否正确:
# listener.ora Network Configuration File: C:\oracle\product\10.2.0\db_1\network\admin\listener.ora
# Generated by Oracle configuration tools.
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = PLSExtProc)
(ORACLE_HOME = C:\oracle\product\10.2.0\db_1)
(PROGRAM = extproc)
)
(SID_DESC =
(GLOBAL_DBNAME=orcl)
(ORACLE_HOME = C:\oracle\product\10.2.0\db_1)
(SID_NAME =orcl)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
)
)
(SID_DESC =
(GLOBAL_DBNAME=orcl)
(ORACLE_HOME = C:\oracle\product\10.2.0\db_1)
(SID_NAME =orcl)
)
# tnsnames.ora Network Configuration File: C:\oracle\product\10.2.0\db_1\network\admin\tnsnames.ora
# Generated by Oracle configuration tools.
ORCL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
EXTPROC_CONNECTION_DATA =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
)
(CONNECT_DATA =
(SID = PLSExtProc)
(PRESENTATION = RO)
)
)
特别是:
ORCL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
查看监听器的状态是否正常:
SQL> host lsnrctl status
LSNRCTL for 32-bit Windows: Version 10.2.0.1.0 - Production on 13-3月 -2014 15:23:31
Copyright (c) 1991, 2005, Oracle. All rights reserved.
正在连接到 (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1)))
LISTENER 的 STATUS
------------------------
别名 LISTENER
版本 TNSLSNR for 32-bit Windows: Version 10.2.0.1.0 - Production
启动日期 13-3月 -2014 14:20:27
正常运行时间 0 天 1 小时 3 分 6 秒
跟踪级别 off
安全性 ON: Local OS Authentication
SNMP OFF
监听程序参数文件 C:\oracle\product\10.2.0\db_1\network\admin\listener.ora
监听程序日志文件 C:\oracle\product\10.2.0\db_1\network\log\listener.log
监听端点概要...
(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(PIPENAME=\\.\pipe\EXTPROC1ipc)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=127.0.0.1)(PORT=1521)))
服务摘要..
服务 "PLSExtProc" 包含 1 个例程。
例程 "PLSExtProc", 状态 UNKNOWN, 包含此服务的 1 个处理程序...
服务 "orcl" 包含 1 个例程。
例程 "orcl", 状态 UNKNOWN, 包含此服务的 1 个处理程序...
命令执行成功
3、设置链接服务器。
需要特别注意的是,数据源orcl指的是:tnsnames.ora文件中的orcl服务名。
第一步,选择“新建链接服务器”:
第二步,一定要选择 Microsoft OLE DB Provide for Oracle:
第三步,输入产品:oracle,数据源:orcl:
第四步,输入oracle的用户名和密码,这个需要根据实际情况设置,输入完后,点确定就好:
4、测试查询
select * from openquery(xxx, 'SELECT * FROM emp')
5、用代码实现:
EXEC master.dbo.sp_addlinkedserver
@server = N'ORACLE_SCOTT',
@srvproduct=N'ORACLE',
@provider=N'MSDAORA',
@datasrc=N'orcl'
go
/* For security reasons the linked server remote logins password is changed with ######## */
EXEC master.dbo.sp_addlinkedsrvlogin
@rmtsrvname=N'ORACLE_SCOTT',
@useself=N'False',
@locallogin=NULL,
@rmtuser=N'scott',
@rmtpassword='tiger'
GO
select * from openquery(xxx, 'SELECT * FROM scott.emp')
6、通过openrowset函数来实现,更简单。
select *
from openrowset('MSDAORA','orcl';'scott';'tiger','select * from SCOTT.EMP')