SQL table macros 13: compare with primary key

I just updated my COMPARE_ROWSETS function to handle an optional primary or unique key.

If that optional parameter is left out, the function uses the venerable GROUP BY method that handles duplicate rows.

If key columns are indicated, then the “Z#_OP” column can contain updates :

  • U: Updated data (as present in p_new_table)
  • O: Old data (as present in p_old_table)

There is no need for a “Z#_CNT” column since there can be no duplicate rows.

Here is an example of use with a key column :

drop table emp2 purge;
 
create table emp2 as select * from emp;
 
update emp2 set ename = ename||' KONG' where rownum = 1;
 
insert into emp2 
select EMPNO+1000, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO
from emp2 where rownum <= 4 and job != 'PRESIDENT';

select * from compare_rowsets(
  emp, emp2,
  p_key_cols => columns(empno),
  p_exclude_cols => columns(comm)
)
order by empno, z#_op;
Z#_OP EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO
O 7839 KING PRESIDENT   1981-11-17 00:00:00 5000 10
U 7839 KING KONG PRESIDENT   1981-11-17 00:00:00 5000 10
I 8566 JONES MANAGER 7839 1981-04-02 00:00:00 2975 20
I 8698 BLAKE MANAGER 7839 1981-05-01 00:00:00 2850 30
I 8782 CLARK MANAGER 7839 1981-06-09 00:00:00 2450 10
I 8788 SCOTT ANALYST 7566 1987-04-19 00:00:00 3000 20
 
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s