【SQL Server数据迁移】64位的机器:SQL Server中查询ORACLE的数据

从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:\Users\Administrator>sqlplus / as sysdba

 SQL*Plus: Release 11.2.0.1. Production on 星期四 3月  :: 

 Copyright (c) , , Oracle.  All rights reserved.

 连接到:
Oracle Database 11g Enterprise Edition Release 11.2.0.1. - 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(*)
----------

查看listener.ora是否正确:

 # listener.ora Network Configuration File: D:\app\Administrator\product\11.2.\dbhome_1\network\admin\listener.ora
# Generated by Oracle configuration tools. SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = CLRExtProc)
(ORACLE_HOME = D:\app\Administrator\product\11.2.\dbhome_1)
(PROGRAM = extproc)
(ENVS = "EXTPROC_DLLS=ONLY:D:\app\Administrator\product\11.2.0\dbhome_1\bin\oraclr11.dll")
) (SID_DESC =
(GLOBAL_DBNAME=orcl)
(ORACLE_HOME = D:\app\Administrator\product\11.2.\dbhome_1)
(SID_NAME =orcl)
)
) LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = ))
)
)

要特别注意里面的:

(SID_DESC =

(GLOBAL_DBNAME=orcl)
      (ORACLE_HOME = D:\app\Administrator\product\11.2.0\dbhome_1)
      (SID_NAME =orcl)
)

另外,查看tnsnames.ora是否正确:

 # tnsnames.ora Network Configuration File: D:\app\Administrator\product\11.2.\dbhome_1\NETWORK\ADMIN\tnsnames.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 = )) orcl =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = ))
(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 -bit Windows: Version 11.2.0.1. - Production on -3月 - ::

 Copyright (c) , , Oracle.  All rights reserved.

 正在连接到 (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=)))
LISTENER 的 STATUS
------------------------
别名 LISTENER
版本 TNSLSNR for -bit Windows: Version 11.2.0.1. - Production
启动日期 -3月 - ::
正常运行时间 天 小时 分 秒
跟踪级别 off
安全性 ON: Local OS Authentication
SNMP OFF
监听程序参数文件 D:\app\Administrator\product\11.2.\dbhome_1\network\admin\listener.ora
监听程序日志文件 d:\app\administrator\diag\tnslsnr\ggg-PC\listener\alert\log.xml
监听端点概要...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=127.0.0.1)(PORT=)))
服务摘要..
服务 "CLRExtProc" 包含 个实例。
实例 "CLRExtProc", 状态 UNKNOWN, 包含此服务的 个处理程序...
服务 "orcl" 包含 个实例。
实例 "orcl", 状态 UNKNOWN, 包含此服务的 个处理程序...
命令执行成功

3、设置链接服务器。

需要特别注意的是,数据源orcl指的是:tnsnames.ora文件中的orcl服务名。

第一步,选择“新建链接服务器”

【SQL Server数据迁移】64位的机器:SQL Server中查询ORACLE的数据

第二步,一定要选择 Oracle Provide for OLE DB

【SQL Server数据迁移】64位的机器:SQL Server中查询ORACLE的数据

第三步,输入产品:oracle,数据源:orcl

【SQL Server数据迁移】64位的机器:SQL Server中查询ORACLE的数据

第四步,输入oracle的用户名和密码,这个需要根据实际情况设置,输入完后,点确定就好。

【SQL Server数据迁移】64位的机器:SQL Server中查询ORACLE的数据

4、测试查询

 

 select * from openquery(xxx, 'SELECT * FROM emp')

返回结果:

【SQL Server数据迁移】64位的机器:SQL Server中查询ORACLE的数据

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 = ?', ) AT ORCL_SCOTT;
GO
DECLARE @v INT;
SET @v = ;
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')

返回结果:

【SQL Server数据迁移】64位的机器:SQL Server中查询ORACLE的数据

上一篇:1、安卓数据存储机制——sharedPreference


下一篇:面向对象_05【类的继承:extends、重写父类】