REM ***********************************************************************
REM Drop tables relating to table differences (to allow re-running entire
REM script with empty "diff" tables).
REM ***********************************************************************
drop table Row_IDs_In_1_not_2;
drop table Row_IDs_In_2_not_1;
drop table diffs;
REM ***********************************************************************
REM Re-create tables relating to table differences
REM ***********************************************************************
create table Row_IDs_In_1_not_2 (id number);
create table Row_IDs_In_2_not_1 (id number);
create table Diffs
(id number
,column_name varchar2(30)
,tab1_value varchar2(4000)
,tab2_value varchar2(4000)
);
REM ***********************************************************************
REM Identify Primary Keys of rows in Tab1 but missing from Tab2
REM ***********************************************************************
insert into Row_IDs_In_1_not_2
(select id from tab1 minus select id from tab2);
REM ***********************************************************************
REM Identify Primary Keys of rows in Tab2 but missing from Tab1
REM ***********************************************************************
insert into Row_IDs_In_2_not_1
(select id from tab2 minus select id from tab1);
REM ***********************************************************************
REM Compares columns in only rows with matching IDs but differing column values
REM ***********************************************************************
declare
t1 tab1%rowtype;
t2 tab2%rowtype;
begin
for r in (select id from -- identifies matching IDs but rows that differ
(select * from tab1
where id not in
(select * from Row_IDs_In_1_not_2)
minus
select * from tab2)) loop
select * into t1 from tab1 where id = r.id;
select * into t2 from tab2 where id = r.id;
if nvl(t1.LAST_NAME,' ') <> nvl(t2.LAST_NAME,' ') then
insert into diffs values (
t1.id,'LAST_NAME',t1.LAST_NAME,t2.LAST_NAME);
end if;
if nvl(t1.FIRST_NAME,' ') <> nvl(t2.FIRST_NAME,' ') then
insert into diffs values (
t1.id,'FIRST_NAME',t1.FIRST_NAME,t2.FIRST_NAME);
end if;
if nvl(t1.USERID,' ') <> nvl(t2.USERID,' ') then
insert into diffs values (
t1.id,'USERID',t1.USERID,t2.USERID);
end if;
if nvl(t1.START_DATE,sysdate-2000000) <>
nvl(t2.START_DATE,sysdate-2000000) then
insert into diffs values (
t1.id,'START_DATE',t1.START_DATE,t2.START_DATE);
end if;
if nvl(t1.COMMENTS,' ') <> nvl(t2.COMMENTS,' ') then
insert into diffs values (
t1.id,'COMMENTS',t1.COMMENTS,t2.COMMENTS);
end if;
if nvl(t1.MANAGER_ID,-1) <> nvl(t2.MANAGER_ID,-1) then
insert into diffs values (
t1.id,'MANAGER_ID',t1.MANAGER_ID,t2.MANAGER_ID);
end if;
if nvl(t1.TITLE,' ') <> nvl(t2.TITLE,' ') then
insert into diffs values (
t1.id,'TITLE',t1.TITLE,t2.TITLE);
end if;
if nvl(t1.DEPT_ID,-1) <> nvl(t2.DEPT_ID,-1) then
insert into diffs values (
t1.id,'DEPT_ID',t1.DEPT_ID,t2.DEPT_ID);
end if;
if nvl(t1.SALARY,-1) <> nvl(t2.SALARY,-1) then
insert into diffs values (
t1.id,'SALARY',t1.SALARY,t2.SALARY);
end if;
if nvl(t1.COMMISSION_PCT,-1) <> nvl(t2.COMMISSION_PCT,-1) then
insert into diffs values (
t1.id,'COMMISSION_PCT',t1.COMMISSION_PCT,t2.COMMISSION_PCT);
end if;
end loop;
COMMIT;
end;
/
REM ***********************************************************************
REM Displays row IDs that reside in Tab1 but not in Tab2
REM ***********************************************************************
col id heading "Ids of rows|in Tab1 but|not in Tab2" format 9999999999
select * from Row_IDs_In_1_not_2;
REM ***********************************************************************
REM Displays row IDs that reside in Tab2 but not in Tab1
REM ***********************************************************************
col id heading "Ids of rows|in Tab2 but|not in Tab1" format 9999999999
select * from Row_IDs_In_2_not_1;
REM ***********************************************************************
REM Displays row IDs, Column names, and Tab1 versus Tab2 values for
REM differing columns only
REM ***********************************************************************
set linesize 200
col id format 9999999999
col column_name format a14
col tab1_value format a25
col tab2_value format a25
select * from diffs;