如何在oracle中比较两个数据库表数据

时间:2016-02-10 12:27:27

标签: database oracle11g

我已经使用oracle数据库实现了这一点,并与源表中的插入,更新和删除数据进行比较,这些数据将同步到目标表。

2 个答案:

答案 0 :(得分:0)

请按照以下步骤

create table tab_comparison_orig
(id number,
first_name varchar2(10),
last_name  varchar2(10));

alter table tab_comparison_orig
 add constraint pk_id primary key (id);


create table tab_comparison_dest
(id number,
first_name varchar2(10),
last_name  varchar2(10));

alter table tab_comparison_dest
 add constraint pk_id_dest primary key (id);



insert into tab_comparison_orig values (1,'Paulo','Portugal');
insert into tab_comparison_orig values (2,'Elber','Portugal');
insert into tab_comparison_orig values (3,'Joao','Alfredo');
commit;

begin
   dbms_comparison.create_comparison(
      comparison_name => 'comp_dbms_test',
      schema_name => 'APP_LCO',
      object_name => 'tab_comparison_orig',
      index_schema_name => 'APP_LCO',
      index_name => 'pk_id',
      dblink_name => NULL,
      remote_schema_name => 'APP_LCO',
      remote_object_name => 'tab_comparison_dest',
     column_list => '*',
     scan_mode => dbms_comparison.cmp_scan_mode_full);
  end;


set serveroutput on
declare
   v_scan_info dbms_comparison.comparison_type;
   v_compare_result boolean;
begin
   v_compare_result:= dbms_comparison.compare(
                     comparison_name => 'comp_dbms_test',
                     scan_info => v_scan_info,
                     perform_row_dif => TRUE);

if v_compare_result = TRUE then
   dbms_output.put_line(a => 'Tables are synchronized!');
else
   dbms_output.put_line(a => 'Warning! Data divergence found!'||chr(10)||
                        'Scan id differences: '||v_scan_info.scan_id);
end if;
end;
/

set serveroutput on
declare 
  v_scan_out dbms_comparison.comparison_type;
begin
  dbms_comparison.converge(
  comparison_name => 'comp_dbms_test',
  scan_id => 2102,
  scan_info => v_scan_out,
  converge_options => dbms_comparison.cmp_converge_local_wins,
  perform_commit => true);
   dbms_output.put_line(a => 'converge scand ID is:'||v_scan_out.scan_id);
   dbms_output.put_line(a => 'local rows updated:'||v_scan_out.loc_rows_merged);
   dbms_output.put_line(a => 'remote rows updated:'||v_scan_out.rmt_rows_merged);
   dbms_output.put_line(a => 'local rows deleted:'||v_scan_out.loc_rows_deleted);
   dbms_output.put_line(a => 'remote rows     deleted:'||v_scan_out.rmt_rows_deleted);      
end;
/

修饰

select TDIF.rowid,TDEST.id,TDEST.First_Name
 from User_Comparison_Row_Dif TDIF, TAB_COMPARISON_DEST TDEST
 where Tdif.Remote_Rowid = TDEST.ROWID
 AND TDIF.Scan_Id IN (select Scan_Id from user_comparison_scan where Root_Scan_Id = v_scan_info.scan_id)
    AND TDIF.Local_Rowid IS NOT NULL;

Delete (brfore sync)
---------------------------
select TDIF.rowid,TDEST.id,TDEST.First_Name
from User_Comparison_Row_Dif TDIF, TAB_COMPARISON_DEST TDEST
where  Tdif.Index_Value = TDEST.id
 AND TDIF.Scan_Id IN (select Scan_Id from user_comparison_scan where    Root_Scan_Id = v_scan_info.scan_id)
AND Tdif.Local_Rowid IS NULL;

Insert + MULTIPLE
------------------------
select TDIF.rowid,TDEST.id,TDEST.First_Name
from User_Comparison_Row_Dif TDIF, TAB_COMPARISON_ORIG TDEST
where  Tdif.Index_Value = TDEST.id
AND TDIF.Scan_Id IN (select Scan_Id from user_comparison_scan where Root_Scan_Id = v_scan_info.scan_id AND Parent_Scan_Id IS NOT NULL)
 AND Tdif.Remote_Rowid IS NULL;

答案 1 :(得分:0)

你可以像

那样做
(select * from table1 minus select * from table2) 
union all
(select * from table2 minus select * from table1) 

这将为您提供两个表中缺少的记录。