本文主要介紹的是如何使Oracle訪問多個SQL Server的實際操作步驟,以及如何在Oracle中設置訪問一些SQL Server相關的數據庫?假設我們要在Oracle裡同時能訪問SQL Server裡默認的pubs和Northwind兩個數據庫。
1、在安裝了Oracle9i Standard Edition或者Oracle9i Enterprise Edition的Windows機器上(IP:192.168.0.2), 產品要選了透明網關(Oracle Transparent Gateway)裡Oracle訪問Microsoft SQL Server數據庫 。
Oracle9I_HOMEtg4msqladmin下新寫initpubs.ora和initnorthwind.ora配置文件.
initpubs.ora內容如下:
- HS_FDS_CONNECT_INFO="SERVER=SQLSERVER_HOSTNMAE; DATABASE=pubs"
- HS_DB_NAME=pubsHS_FDS_TRACE_LEVEL=OFF
- HS_FDS_RECOVERY_ACCOUNT=RECOVER
- HS_FDS_RECOVERY_PWD=RECOVER
- initnorthwind.ora內容如下:
- HS_FDS_CONNECT_INFO="SERVER=sqlserver_hostname; DATABASE=Northwind"
- HS_DB_NAME=Northwind
- HS_FDS_TRACE_LEVEL=OFF
- HS_FDS_RECOVERY_ACCOUNT=RECOVER
- HS_FDS_RECOVERY_PWD=RECOVER
- $Oracle9I_HOMEnetworkadmin 下listener.ora內容如下:
- LISTENER =
- (DESCRIPTION_LIST =
- (DESCRIPTION =
- (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)
(HOST = 192.168.0.2)(PORT = 1521))- )
- )
- )
- SID_LIST_LISTENER =
- (SID_LIST =
- (SID_DESC =
- (GLOBAL_DBNAME = test9)
- (Oracle_HOME = d:Oracleora92)
- (SID_NAME = test9)
- )
- (SID_DESC=
- (SID_NAME=pubs)
- (Oracle_HOME=d:OracleOra92)
- (PROGRAM=tg4msql)
- )
- (SID_DESC=
- (SID_NAME=northwind)
- (Oracle_HOME=d:OracleOra92)
- (PROGRAM=tg4msql)
- )
- )
重啟動這台做gateway的Windows機器上(IP:192.168.0.2)TNSListener服務(凡是按此步驟新增可Oracle訪問的SQL Server數據庫時,TNSListener服務都要重啟動)。
2、Oracle8i,Oracle9i的服務器端配置tnsnames.ora, 添加下面的內容:
- pubs =
- (DESCRIPTION =
- (ADDRESS_LIST =
- (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.2)(PORT = 1521))
- )
- (CONNECT_DATA =
- (SID = pubs)
- )
- (HS = pubs)
- )
- northwind =
- (DESCRIPTION =
- (ADDRESS_LIST =
- (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.2)(PORT = 1521))
- )
- (CONNECT_DATA =
- (SID = northwind)
- )
- (HS = northwind) )
保存tnsnames.ora後,在命令行下
- tnsping pubs
- tnsping northwind
出現類似提示,即為成功:
- Attempting to contact (DESCRIPTION = (ADDRESS_LIST =
- (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.2)
- (PORT = 1521))) (CONNECT_DATA = (SID = pubs)) (HS = pubs))
- OK(20毫秒)
- Attempting to contact (DESCRIPTION = (ADDRESS_LIST =
- (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.2)
- (PORT = 1521)))
(CONNECT_DATA = (SID = northwind)) (HS = northwind))OK(20毫秒)
設置數據庫參數global_names=false。
設置global_names=false不要求建立的數據庫鏈接和目的數據庫的全局名稱一致。global_names=true則要求, 多少有些不方便。
oracle9i和Oracle8i都可以在DBA用戶下用SQL命令改變global_names參數
- alter system set global_names=false;
建立公有的數據庫鏈接:
- create public database link pubs
- connect to testuser identifIEd by testuser_pwd using 'pubs';
- create public database link northwind
- connect to testuser identifIEd by testuser_pwd using 'northwind';
(假設SQL Server下pubs和northwind已有足夠權限的用戶登陸testuser,密碼為testuser_pwd)
訪問SQL Server下數據庫裡的數據:
- select * from stores@pubs;
- ...... ......select *
- from region@northwind;
3、使用時的注意事項
ORACLE通過Oracle訪問SQL Server的數據庫鏈接時,用select * 的時候字段名是用雙引號引起來的。
例如:
ERROR 位於第 1 行:
ORA-00904: 無效列名
已選擇6行,用SQL Navigator或Toad看從SQL Server轉移到Oracle裡的表的建表語句為:
- CREATE TABLE stores
- ("stor_id" CHAR(4) NOT NULL,
- "stor_name" VARCHAR2(40),
- "stor_address" VARCHAR2(40),
- "city" VARCHAR2(20),
- "state" CHAR(2),
- "zip" CHAR(5))
- PCTFREE 10
- PCTUSED 40
- INITRANS 1
- MAXTRANS 255
- TABLESPACE users
- STORAGE (
- INITIAL 131072
- NEXT 131072
- PCTINCREASE 0
- MINEXTENTS 1
- MAXEXTENTS 2147483645
- )