利用dblink實現Oracle對Oracle資料庫的同步

來源:互聯網
上載者:User

利用dblink 做Oracle到oracle的定時同步,以剛為卡中心做的一個同步為例

推薦閱讀:

利用Oracle DBLink進行表同步

Oracle DBLink使用

Oracle通過DBLink訪問GreenPlum 

ORA-01017/ORA-02063 DBLink建立錯誤問題分析及解決

Oracle 建立DBLink 報錯:ORA-01017、ORA-02063

1、建立需要的dblink串連的資料庫(若已經有,則不需要)
 
例:
查看 dblink :  select * from dba_db_links;
 
建立 dblink :
 
create database link ECARDRYXX
  connect to WPENG
  using '(DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 202.120.85.118)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SID = ORA92)
    )
  )';
 
 
2. 建立需要同步到對方資料的視圖:
 
例:
create or replace view ryxx as
select ryjbxx.userid outid, xyzzjg.bmmc college, xszzjg.bmmc departname, ryjbxx.major,
      ryjbxx.typeof, ryjbxx.sfsf, ryjbxx.cn, ryjbxx.email, decode(sys_user.STATUS, '2', 'Yes', '4', 'No', 'No') inetUserStatus
      from
(
select t1.xh userid, t1.xym , t1.xsm , t2.zymc major,
      'student' typeof, t1.sfsf sfsf, t1.xm cn, t1.dzxx email
      from idc_u_xs.xs_xsjbxx t1, idc_u_jwmis.jw_zyxx t2
      where  t1.zyh = t2.zydh(+)                                                -- 本科生
union all
select t1.xh userid, t1.xym, t1.xsm, t2.zymc, 'student' typeof, '' sfsf, t1.xm cn, t1.email email
      from idc_u_yjs.yjs_yjsjbxx t1, idc_u_yjs.yjs_dict_bsxkzydm t2
      where t1.zydm = t2.zydm(+)                                                  -- 研究生
union all
select t1.zgh userid, '' xym, '' xsm, '' major, 'teacher' typeof, '' sfsf,  t1.xm cn, t1.dzxx email
      from idc_u_rs.rs_zzryjbxx t1                                                --  在職人員
union all
select t1.zgh userid, '' xym, '' xsm, '' major, 'teacher' typeof, '' sfsf,  t1.xm cn, t1.dzxx email
      from idc_u_rs.rs_xjrylsb t1                                                -- 新增人員
) ryjbxx, idc_u_pub.sys_user, idc_u_rs.rs_zzjg xyzzjg, idc_u_rs.rs_zzjg xszzjg
where
      ryjbxx.userid = sys_user.userid
      and ryjbxx.xym = xyzzjg.bmbh(+)    --  學院組織機構
      and ryjbxx.xym = xszzjg.bmbh(+)    --  院系組織機構

3. 建立需要執行同步的pl/sql :
 
例如:
  為了使用更模組化,更集中,建議同面向同一個庫的pl/sql,寫在同一個包下。
 
CREATE OR REPLACE PACKAGE ECARD IS
      PROCEDURE RYXXSYNC;
END;
 
CREATE OR REPLACE PACKAGE BODY ECARD IS
      PROCEDURE RYXXSYNC IS
      BEGIN
            delete from wpeng.ryxx@ecardryxx; --刪除資料
            insert into wpeng.ryxx@ecardryxx(outid, college, departname, major, typeof, sfsf, cn, email, inetuserstatus)
                  select outid, college, departname, major, typeof, sfsf, cn, email, inetuserstatus from ryxx;  --插入最新資料
      END;
END;
 

4. 建立job,定時執行同步:
 
例:
開啟 sql命令視窗:
 
variable ryxxsyncjob number;
 
 
begin
    dbms_job.submit(:ryxxsyncjob, 'ECARD.RYXXSYNC;',
                                  SYSDATE, 'TRUNC(SYSDATE) + 1 + 2/24');  --每天淩晨兩點執行。
    commit;
end;

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.