Oracle dblink比較兩個庫中的表欄位

zhenghaishu發表於2015-11-16

Oracle dblink比較兩個庫中的表欄位

 

1 A庫IP為10.0.30.65,olduser使用者下有個表oldtable,該表只有一個列oldid。

B庫IP為10.0.30.64,newuser使用者下有個表newtable,該表只有一個列newid。

 

在A庫中執行

create user olduser identified by old123;

grant connect, resource to olduser;

connect olduser/old123;

create table oldtable(oldid number);

insert into oldtable values(1);

insert into oldtable values(2);

insert into oldtable values(3);

commit;

 

在B庫中執行

create user newuser identified by new123;

grant connect, resource to newuser;

connect newuser/new123;

create table newtable(newid number);

insert into newtable values(2);

insert into newtable values(3);

insert into newtable values(4);

commit;

 

2 B庫的/u01/app/oracle/product/11.2.0/db_1/network/admin/tnsnames.ora中配置連線A庫的網路服務名old

OLD =

  (DESCRIPTION =

    (ADDRESS = (PROTOCOL = TCP)(HOST = 10.0.30.65)(PORT = 1521))

    (CONNECT_DATA =

      (SERVER = DEDICATED)

      (SERVICE_NAME = orcl)

    )

  )

 

3 在B庫中用system帳戶建立遠端連線A庫的dblink

conn system/oracle

create public database link old_db connect to olduser identified by old123 using 'old';

 

4 查詢newtable中沒有而oldtable中有的欄位

conn newuser/new123;

select oldid from oldtable@old_db where oldid not in (select newid from newtable);

     OLDID

----------

       1

 

5 查詢oldtable中沒有而newtable中有的欄位

 select newid from newtable where newid not in (select oldid from oldtable@old_db);

     NEWID

----------

       4

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29485627/viewspace-1839762/,如需轉載,請註明出處,否則將追究法律責任。

相關文章