如何在 SQL 2000/2005 上设置连接到 Oracle 数据库的链接服务器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/307636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you setup a linked server to an Oracle database on SQL 2000/2005?
提问by Oppositional
I am able to create and execute a DTS package that copies tables from a remote Oracle database to a local SQL server, but want to setup the connection to the Oracle database as a linked server.
我能够创建并执行一个 DTS 包,该包将表从远程 Oracle 数据库复制到本地 SQL 服务器,但希望将与 Oracle 数据库的连接设置为链接服务器。
The DTS package currently uses the Microsoft OLE DB Provider for Oraclewith the following properties:
DTS 包当前使用Microsoft OLE DB Provider for Oracle,具有以下属性:
- Data Source:
SERVER=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=10.1.3.42)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=acc)));uid=*UserName*;pwd=*UserPassword*;
- Password: UserPassword
- User ID: UserName
- Allow saving password: true
- 数据源:
SERVER=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=10.1.3.42)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=acc)));uid=*UserName*;pwd=*UserPassword*;
- 密码:userPassword的
- 用户名:用户名
- 允许保存密码:true
How do I go about setting a linked server to an Oracle database using the data source defined above?
如何使用上面定义的数据源将链接服务器设置为 Oracle 数据库?
回答by Oppositional
I was able to setup a linked server to a remote Oracle database, which ended up being a multi-step process:
我能够将链接服务器设置为远程 Oracle 数据库,这最终是一个多步骤过程:
- Install Oracle ODBC drivers on SQL Server.
- Create System DSN to Oracle database on SQL Server.
- Create linked server on SQL server using System DSN.
- 在 SQL Server 上安装 Oracle ODBC 驱动程序。
- 在 SQL Server 上创建系统 DSN 到 Oracle 数据库。
- 使用系统 DSN 在 SQL 服务器上创建链接服务器。
Step 1: Install Oracle ODBC drivers on server
步骤 1:在服务器上安装 Oracle ODBC 驱动程序
a. Download the necessary Oracle Instant Clientpackages: Basic, ODBC, and SQL*Plus (optional)
一种。下载必要的Oracle Instant Client包:Basic、ODBC 和 SQL*Plus(可选)
b. Unzip the packages to a local directory on the SQL server, typically C:\Oracle
. This should result in a [directory] like C:\Oracle\instantclient_10_2
, which will be the value of [directory] referenced in the rest of this answer.
湾 将包解压缩到 SQL 服务器上的本地目录,通常是C:\Oracle
. 这应该会产生一个 [directory] like C:\Oracle\instantclient_10_2
,这将是本答案其余部分中引用的 [directory] 的值。
c. Create a text file named tnsnames.ora
within the instant client [directory] that contains the following:
C。创建一个tnsnames.ora
在即时客户端 [目录] 中命名的文本文件,其中包含以下内容:
OracleTnsName =
(
DESCRIPTION=
(
ADDRESS = (PROTOCOL=TCP)(HOST=10.1.3.42)(PORT=1521)
)
(
CONNECT_DATA = (SERVICE_NAME=acc)
)
)
Note: Actual HOST
, PORT
, and SERVICE_NAME
will vary based on Oracle server you are establishing a connection to. This information can often be found using the Oracle network client tools under the listeners.
注意:实际HOST
、PORT
和SERVICE_NAME
将根据您要建立连接的 Oracle 服务器而有所不同。通常可以使用侦听器下的 Oracle 网络客户端工具找到此信息。
The OracleTnsName
can be any name you want to assign to the Oracle data source, and will be used when setting up the system DSN. You can also use the syntax above to define multiple TNS names in the same tnsnames.orafile if desired.
该OracleTnsName
可你要分配给Oracle数据源的任何名称,设置系统DSN时将被使用。如果需要,您还可以使用上述语法在同一个tnsnames.ora文件中定义多个 TNS 名称。
d. Add the [directory] to the system PATH
environment variable.
d. 将[目录]添加到系统PATH
环境变量中。
e. Create a new system environment variable named TNS_Admin
that has a value of [directory]
e. 创建一个名为TNS_Admin
[directory]的新系统环境变量
f. Execute the [directory]\odbc_install.exe
utility to install the Oracle ODBC drivers.
F。执行该[directory]\odbc_install.exe
实用程序以安装 Oracle ODBC 驱动程序。
g. It is recommended that you reboot the SQL server, but may not be necessary. Also, you may want to grant security permissions to this directory for the SQL server and SQL agent user identities.
G。建议您重新启动 SQL 服务器,但可能没有必要。此外,您可能希望为 SQL 服务器和 SQL 代理用户标识授予对该目录的安全权限。
Step 2: Create a System DNS that uses the Oracle ODBC driver
步骤 2:创建使用 Oracle ODBC 驱动程序的系统 DNS
a. Open the ODBC Data Source Administratortool. [ Administrative Tools --> Data Sources (ODBC) ]
一种。打开ODBC 数据源管理器工具。[ 管理工具 --> 数据源 (ODBC) ]
b. Select the System DSN tab and then select the Add button.
湾 选择系统 DSN 选项卡,然后选择添加按钮。
c. In the drivers list, select Oracle in instantclient {version}. (e.g. 'Oracle in instantclient 10_2') and then select Finish button.
C。在驱动程序列表中,选择Instantclient {version} 中的 Oracle。(例如'Oracle in instantclient 10_2'),然后选择完成按钮。
d. Specify the following:
d. 指定以下内容:
Data Source Name
: {System DSN Name}Description
: {leave blank/empty}TNS Service Name
: should have theOracleTnsName
you defined in thetnsnames.ora
file listed, select it as the value.- User ID: {Oracle user name}
Data Source Name
: {系统 DSN 名称}Description
:{留空/空}TNS Service Name
: 应该OracleTnsName
在tnsnames.ora
列出的文件中定义了你,选择它作为值。- 用户 ID:{Oracle 用户名}
e. Select Test Connection button. You should be prompted to provide the {Oracle user password}. If all goes well the test will succeed.
e. 选择测试连接按钮。系统会提示您提供 {Oracle 用户密码}。如果一切顺利,测试就会成功。
Step 3: Create linked server in SQL to the Oracle database
第 3 步:在 SQL 中创建链接服务器到 Oracle 数据库
Open a query window in SQL server and execute the following:
在 SQL Server 中打开一个查询窗口并执行以下操作:
EXEC sp_addlinkedserver
@server = '{Linked Server Name}'
,@srvproduct = '{System DSN Name}'
,@provider = 'MSDASQL'
,@datasrc = '{System DSN Name}'
EXEC sp_addlinkedsrvlogin
@rmtsrvname = '{Linked Server Name}'
,@useself = 'False'
,@locallogin = NULL
,@rmtuser = '{Oracle User Name}'
,@rmtpassword = '{Oracle User Password}'
Note: The {Linked Server Name}
can be anything you want to use when referencing the Oracle server, but the {System DNS Name}
mustmatch the name of the system DSN you created previously.
注意:{Linked Server Name}
可以是引用 Oracle 服务器时您想使用的任何内容,但{System DNS Name}
必须与您之前创建的系统 DSN 的名称相匹配。
The {Oracle User Name}
should be the same as the User ID used by the system DSN, and the {Oracle User Password}
should be the same as you used to successfully test the ODBC connection. See KB 280106for information on troubleshooting Oracle linked server issues.
本{Oracle User Name}
应该是相同的,通过该系统DSN使用的用户ID,并{Oracle User Password}
应该与你用来成功地测试ODBC连接相同。有关对 Oracle 链接服务器问题进行故障排除的信息,请参阅知识库文章 280106。
Querying the Oracle linked server
查询 Oracle 链接服务器
You may use OPENQUERYto execute pass-through queries on the Oracle linked server, but be aware that for very large recordsets you may receive a ORA-01652
error message if you specify a ORDER BY
clause in the pass-through query. Moving the ORDER BY
clause from the pass-through query to the outer select statement solved this issue for me.
您可以使用OPENQUERY在 Oracle 链接服务器上执行传递查询,但请注意,对于非常大的记录集,ORA-01652
如果您ORDER BY
在传递查询中指定子句,您可能会收到一条错误消息。将ORDER BY
子句从传递查询移动到外部选择语句为我解决了这个问题。
回答by Oppositional
I had the same problem. I was on the phone with Microsoft for hours, and they did not have a solution. None of those "connection timeout" settings helped me.
我有同样的问题。我和微软通了几个小时的电话,他们没有解决办法。这些“连接超时”设置都没有帮助我。
To resolve it, I created a DTS job that runs a proc which only updates the time on one row, in one column, every two minutes. Then I setup a replication between SQL Server and Oracle, scheduled to replicate that single cell change, from SQL to Oracle, every 3 minutes. It keeps the connection alive!
为了解决这个问题,我创建了一个 DTS 作业,它运行一个 proc,它每两分钟只更新一行、一列的时间。然后我在 SQL Server 和 Oracle 之间设置了一个复制,计划每 3 分钟复制一次从 SQL 到 Oracle 的单个单元更改。它使连接保持活动状态!