【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,并给账户解锁,尝试登录,查询表:

 1 C:\Users\Administrator>sqlplus / as sysdba
 2  
 3 SQL*Plus: Release 11.2.0.1.0 Production on 星期四 3月 13 16:11:03 2014
 4  
 5 Copyright (c) 1982, 2010, Oracle.  All rights reserved.
 6  
 7  
 8 连接到:
 9 Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
10 With the Partitioning, OLAP, Data Mining and Real Application Testing options
11  
12 SQL> alter user scott identified by tiger;
13  
14 用户已更改。
15  
16 SQL> alter user scott account unlock;
17  
18 用户已更改。
19  
20 SQL> connect scott/tiger
21 已连接。
22 SQL> select count(*) from emp;
23   COUNT(*)
24 ----------
25    12

 查看listener.ora是否正确:

 1 # listener.ora Network Configuration File: D:\app\Administrator\product\11.2.0\dbhome_1\network\admin\listener.ora
 2 # Generated by Oracle configuration tools.
 3  
 4  
 5 SID_LIST_LISTENER =
 6   (SID_LIST =
 7     (SID_DESC =
 8       (SID_NAME = CLRExtProc)
 9       (ORACLE_HOME = D:\app\Administrator\product\11.2.0\dbhome_1)
10       (PROGRAM = extproc)
11       (ENVS = "EXTPROC_DLLS=ONLY:D:\app\Administrator\product\11.2.0\dbhome_1\bin\oraclr11.dll")
12     )
13  
14     (SID_DESC =
15       (GLOBAL_DBNAME=orcl)
16       (ORACLE_HOME = D:\app\Administrator\product\11.2.0\dbhome_1)
17       (SID_NAME =orcl)
18     )
19   )
20  
21  
22  
23 LISTENER =
24   (DESCRIPTION_LIST =
25     (DESCRIPTION =
26       (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
27     )
28   )

要特别注意里面的:

 (SID_DESC =

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


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

 1 # tnsnames.ora Network Configuration File: D:\app\Administrator\product\11.2.0\dbhome_1\NETWORK\ADMIN\tnsnames.ora
 2 # Generated by Oracle configuration tools.
 3  
 4 ORACLR_CONNECTION_DATA =
 5   (DESCRIPTION =
 6     (ADDRESS_LIST =
 7       (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
 8     )
 9     (CONNECT_DATA =
10       (SID = CLRExtProc)
11       (PRESENTATION = RO)
12     )
13   )
14  
15 LISTENER_orcl =
16   (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
17  
18 orcl =
19   (DESCRIPTION =
20     (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
21     (CONNECT_DATA =
22       (SID = orcl)
23       (SERVER = DEDICATED)
24       (SERVICE_NAME = orcl)
25     )
26   )

特别是:

orcl =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
    (CONNECT_DATA =
      (SID = orcl)
      (SERVER = DEDICATED)
      (SERVICE_NAME = orcl)
    )
  )


查看监听器的状态是否正常:

 1 SQL> host lsnrctl status
 2  
 3 LSNRCTL for 64-bit Windows: Version 11.2.0.1.0 - Production on 13-3月 -2014 16:16:21
 4  
 5 Copyright (c) 1991, 2010, Oracle.  All rights reserved.
 6  
 7 正在连接到 (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))
 8 LISTENER 的 STATUS
 9 ------------------------
10 别名                      LISTENER
11 版本                      TNSLSNR for 64-bit Windows: Version 11.2.0.1.0 - Production
12 启动日期                  13-3月 -2014 13:17:27
13 正常运行时间              02 小时 585814 跟踪级别                  off
15 安全性                    ON: Local OS Authentication
16 SNMP                      OFF
17 监听程序参数文件          D:\app\Administrator\product\11.2.0\dbhome_1\network\admin\listener.ora
18 监听程序日志文件          d:\app\administrator\diag\tnslsnr\ggg-PC\listener\alert\log.xml
19 监听端点概要...
20   (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=127.0.0.1)(PORT=1521)))
21 服务摘要..
22 服务 "CLRExtProc" 包含 1 个实例。
23   实例 "CLRExtProc", 状态 UNKNOWN, 包含此服务的 1 个处理程序...
24 服务 "orcl" 包含 1 个实例。
25   实例 "orcl", 状态 UNKNOWN, 包含此服务的 1 个处理程序...
26 命令执行成功

3、设置链接服务器。

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

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

 

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

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

 

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

 

 4、测试查询

 

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

返回结果:

5、用代码实现:

 1 /****** Object:  LinkedServer [ORCL_SCOTT]    Script Date: 03/13/2014 16:25:21 ******/
 2 EXEC master.dbo.sp_addlinkedserver 
 3        @server = N'ORCL_SCOTT', 
 4        @srvproduct=N'oracle', 
 5        @provider=N'OraOLEDB.Oracle', 
 6        @datasrc=N'orcl'
 7 GO
 8        
 9  /* For security reasons the linked server remote logins password is changed with ######## */
10 EXEC master.dbo.sp_addlinkedsrvlogin 
11        @rmtsrvname=N'ORCL_SCOTT',
12        @useself=N'False',
13        @locallogin=NULL,
14        @rmtuser=N'scott',
15        @rmtpassword='tiger'
16 go
17  
18  
19 select * from openquery(ORCL_SCOTT, 'SELECT * FROM scott.emp')

还可以用?表示绑定变量:

1 EXEC ( 'SELECT * FROM scott.emp') AT ORCL_SCOTT;
2 GO
3 EXEC ( 'SELECT * FROM scott.emp WHERE MGR = ?', 7902) AT ORCL_SCOTT;
4 GO
5 DECLARE @v INT; 
6 SET @v = 7902;
7 EXEC ( 'SELECT * FROM scott.emp WHERE MGR = ?', @v) AT ORCL_SCOTT;
8 GO 

6、通过openrowset函数来实现,更简单。

1 select *  from openrowset('OraOLEDB.Oracle','orcl';'scott';'tiger','select * from SCOTT.EMP')

返回结果:

猜你喜欢

转载自www.cnblogs.com/lgx5/p/11365016.html