熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Oracle >> 正文

訪問多個SQL Server數據庫

2022-06-13   來源: Oracle 

  假設我們要在ORACLE裡同時能訪問SQL Server裡默認的pubs和Northwind兩個數據庫
  
  在安裝了ORACLEi Standard Edition或者ORACLEi Enterprise Edition的windows機器上(IP:)
    產品要選了透明網關(Oracle Transparent Gateway)裡要訪問Microsoft SQL Server數據庫
  
  $ORACLEI_HOME\tgmsql\admin下新寫initpubsora和initnorthwindora配置文件
  initpubsora內容如下:
  HS_FDS_CONNECT_INFO=SERVER=sqlserver_hostname;DATABASE=pubs
  HS_DB_NAME=pubs
  HS_FDS_TRACE_LEVEL=OFF
  HS_FDS_RECOVERY_ACCOUNT=RECOVER
  HS_FDS_RECOVERY_PWD=RECOVER
  
  initnorthwindora內容如下:
  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
  
  (藍色字的部分可以根據具體要訪問的SQL Server數據庫的情況而修改)
  
  $ORACLEI_HOME\network\admin 下listenerora內容如下:
  
  LISTENER =
   (DESCRIPTION_LIST =
    (DESCRIPTION =
     (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = )(PORT = ))
     )
    )
   )
  
  SID_LIST_LISTENER =
   (SID_LIST =
    (SID_DESC =
     (GLOBAL_DBNAME = test)
     (ORACLE_HOME = d:\oracle\ora)
     (SID_NAME = test)
    )
    (SID_DESC=
     (SID_NAME=pubs)
     (ORACLE_HOME=d:\Oracle\Ora)
     (PROGRAM=tgmsql)
    )
    (SID_DESC=
     (SID_NAME=northwind)
     (ORACLE_HOME=d:\Oracle\Ora)
     (PROGRAM=tgmsql)
    )
   )
  
   重啟動這台做gateway的windows機器上(IP:)TNSListener服務
  
   (凡是按此步驟新增可訪問的SQL Server數據庫時TNSListener服務都要重啟動)
  
   ORACLEIORACLEI的服務器端配置tnsnamesora 添加下面的內容:
  
  pubs =
   (DESCRIPTION =
   (ADDRESS_LIST =
     (ADDRESS = (PROTOCOL = TCP)(HOST = )(PORT = ))
    )
    (CONNECT_DATA =
     (SID = pubs)
    )
    (HS = pubs)
    )
   
  northwind =
   (DESCRIPTION =
   (ADDRESS_LIST =
     (ADDRESS = (PROTOCOL = TCP)(HOST = )(PORT = ))
    )
    (CONNECT_DATA =
     (SID = northwind)
    )
    (HS = northwind)
    ) 
   
    保存tnsnamesora後在命令行下
   
    tnsping pubs
    tnsping northwind
   
   出現類似提示即為成功
   Attempting to contact (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = )
   (PORT = ))) (CONNECT_DATA = (SID = pubs)) (HS = pubs))
   OK(毫秒)
   
    Attempting to contact (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = )
   (PORT = ))) (CONNECT_DATA = (SID = northwind)) (HS = northwind))
   OK(毫秒)
  
   設置數據庫參數global_names=false
  
   設置global_names=false不要求建立的數據庫鏈接和目的數據庫的全局名稱一致
   global_names=true則要求 多少有些不方便
  
   oraclei和oraclei都可以在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;
    
  
   使用時的注意事項
  
   ORACLE通過訪問SQL Server的數據庫鏈接時用select * 的時候字段名是用雙引號引起來的
  
   例如
   create table stores as select * from stores@pubs;
  
   select zip from stores
  
   ERROR 位於第 行:
   ORA: 無效列名
  
   select zip from stores;
  
   zip
  
  
  
  
  
  
  
  
   已選擇
  
   用SQL Navigator或Toad看從SQL Server轉移到ORACLE裡的表的建表語句為:
  
  CREATE TABLE stores
    (stor_id           CHAR() NOT NULL
    stor_name          VARCHAR()
    stor_address         VARCHAR()
    city             VARCHAR()
    state            CHAR()
    zip             CHAR())
   PCTFREE  
   PCTUSED  
   INITRANS  
   MAXTRANS  
   TABLESPACE users
   STORAGE  (
    INITIAL  
    NEXT    
    PCTINCREASE
    MINEXTENTS 
    MAXEXTENTS 
   )
  /
  
  總結: WINDOWS下ORACLEi網關服務器在$ORACLEI_HOME\tgmsql\admin目錄下的initsqlserver_databaseidora
  
   WINDOWS下ORACLEi網關服務器listenerora裡面
     (SID_DESC=
      (SID_NAME=sqlserver_databaseid)
      (ORACLE_HOME=d:\Oracle\Ora)
      (PROGRAM=tgmsql)
     )
    
     UNIX或WINDOWS下ORACLEIORACLEI服務器tnsnamesora裡面
   northwind =
    (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = )(PORT = ))
     )
     (CONNECT_DATA =
      (SID = sqlserver_databaseid)
     )
     (HS = sqlserver_databaseid)
      )
         
     sqlserver_databaseid一致才行
From:http://tw.wingwit.com/Article/program/Oracle/201311/17434.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.