ORACLE query data from the SQL Server, you can create in SQL Server to ORACLE linked server to achieve, but according to 32-bit, 64-bit machines and software, you need to implement a different driver.
On 32-bit machines, by accessing the interface: Microsoft OLE DB Provide for Oracle, to achieve.
1, machine and software environment
The machine is a virtual machine, the installation of windows xp, SQL Server 20008R2, Oracle 10g 10.2.0.1.0.
2, ORACLE environment settings
Connect 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
Modify the password for the user scott tiger, and to unlock the account, try to sign in, look-up table:
SQL> alter user scott identified by tiger ; the user has changed. SQL> alter user scott account unlock; users have been changed. SQL> connect scott / tiger Connected. SQL> the SELECT COUNT (*) from emp; COUNT (*) ---------- 14
View listener.ora is correct:
# 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)) ) )
Pay particular attention to inside:
(SID_DESC =
(GLOBAL_DBNAME = orcl)
(ORACLE_HOME = C: \ the Oracle \ Product \ 10.2.0 \ db_1)
(SID_NAME = orcl)
)
In addition, see the tnsnames.ora is correct:
# 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> lsnrctl Host Status LSNRCTL for 32-bit Windows: Version 10.2.0.1.0 - Production's ON 13- March 15:23:31 -2014 .. Copyright (c) 1991, 2005, the Oracle All Rights Reserved Connecting to ( = the DESCRIPTION (ADDRESS = (the PROTOCOL = the IPC) (KEY = EXTPROC1))) LISTENER of the STATUS ------------------------ alias LISTENER version TNSLSNR for 32 -bit Windows: Version 10.2.0.1.0 - Production start date 13- March -2014 14:20:27 uptime 0 days 1 hour 3 minutes 6 seconds to track levels off security ON: Local OS Authentication the SNMP OFF listener parameter file C: \ oracle \ product \ 10.2.0 \ db_1 \ network \ admin \ listener.ora Listener log file C: \ oracle \ product \ 10.2.0 \ db_1 \ network \ log \ listener.log listening endpoint summary ... (the DESCRIPTION = (ADDRESS = (PROTOCOL = ipc) (PipeName = \\ \ pipe \. EXTPROC1ipc))) (the DESCRIPTION = (ADDRESS = (the PROTOCOL = TCP) (the HOST = 127.0.0.1) (PORT = 1521))) and services .. Abstract service "PLSExtProc" contains a routine. Routine "PLSExtProc", status UNKNOWN, this service includes a handler ... Services "orcl" contains a routine. Routine "orcl", status UNKNOWN, contains a handler for this service ... The command completed successfully
3, set up a linked server.
Of particular note is that the data source orcl refers: tnsnames.ora file orcl service name.
The first step, select "New Linked Server":
The second step, be sure to select Microsoft OLE DB Provide for Oracle:
The third step, enter the product: oracle, Data Source: orcl:
The fourth step, enter oracle user name and password, this setting according to the actual situation, after the input, determine the points like:
4, test queries
select * from openquery(xxx, 'SELECT * FROM emp')
Return result:
5, implemented in code:
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(ORACLE_SCOTT, 'SELECT * FROM scott.emp')
6、通过openrowset函数来实现,更简单。
select * from openrowset('MSDAORA','orcl';'scott';'tiger','select * from SCOTT.EMP')