Why not to use one of supplied packages. DBMS_COMPARISON
The Package allows to compare and sync tables. It's only required that tables have an index.
1) create diff datasets
create table to_compare2 as (select OBJECT_NAME, SUBOBJECT_NAME, OBJECT_ID, DATA_OBJECT_ID, OBJECT_TYPE, case when mod(object_id,18) = 0 then CREATED +1 else CREATED end CREATED from all_objects where mod(object_id,6) = 0 );
CREATE table to_compare1 as (SELECT OBJECT_NAME, SUBOBJECT_NAME, OBJECT_ID, DATA_OBJECT_ID, OBJECT_TYPE, case when mod(object_id,12) = 0 then CREATED +1 else CREATED end CREATED FROM ALL_OBJECTS where mod(object_id,3) = 0 );
2) create indexes.
CREATE UNIQUE INDEX to_compare1_idx on to_compare1(object_id);
CREATE UNIQUE INDEX to_compare2_idx on to_compare2(object_id);
3) Prepare comparision context
BEGIN
DBMS_COMPARISON.create_comparison (
comparison_name => 'MY_COMPARISION',
schema_name => user,
object_name => 'to_compare1',
dblink_name => NULL,
remote_schema_name => null,
remote_object_name => 'to_compare2');
END;
/
4) Execute comparison and check results.
DECLARE
v_scan_info DBMS_COMPARISON.comparison_type;
v_result BOOLEAN;
BEGIN
v_result := DBMS_COMPARISON.compare (
comparison_name => 'MY_COMPARISION',
scan_info => v_scan_info,
perform_row_dif => TRUE
);
IF NOT v_result THEN
DBMS_OUTPUT.put_line('Differences. scan_id=' || v_scan_info.scan_id);
ELSE
DBMS_OUTPUT.put_line('No differences.');
END IF;
END;
/
4) Results
SELECT *
FROM user_comparison_row_dif
WHERE comparison_name = 'MY_COMPARISION';
if local_rowid
is not null and remote_rowid
is null -> record exit in table_1
if local_rowid
is null and remote_rowid is
not null -> record exit in table_2
if local_rowid
is not null and remote_rowid
is not null -> record exist in both tables but it has different values