Skip to Main Content
  • Questions
  • Compare selected columns of two tables

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Lakshmi.

Asked: February 26, 2013 - 2:38 am UTC

Last updated: March 05, 2013 - 1:17 pm UTC

Version: 11.2.0.2

Viewed 1000+ times

You Asked

Hi Tom

We have tables in two different databases and we would like to compare them using DBMS_COMPARISION package. The tables have standard WHO columns(creation_date,update_date,user) and this value will be different for the two tables. I know we can use a single table view to exclude these columns and do a comparison of the tables using DBMS_COMPARISION. Since we have thousands of tables on our warehouse this method could be bit cumbersome. I am wondering if you can help me to write a code which will ignore this WHO columns and do the DBMS_COMPARISION for other columns(Avoid creating a view manually).

1) Example
Database -1
Employee table
ID NUMBER
NAME VARCHAR2(100)
DEPT NUMBER
GRADE VARCHAR2(10)
CREATION_DATE DATE
UPDATE_DATE DATE
MODIFIED_BY VARCHAR2(100)

Database 2
Employee table
ID NUMBER
NAME VARCHAR2(100)
DEPT NUMBER
GRADE VARCHAR2(10)
CREATION_DATE DATE
UPDATE_DATE DATE
MODIFIED_BY VARCHAR2(100)

In the above example I need to compare columns ID,NAME,DEPT,GRADE. The standard WHO columns will be the same for ALL tables inside the database.

Appreciate your help.

Thanks
Lsanthan

and Tom said...

dbms_comparison.CREATE_COMPARISON takes a column list as input - you just need to supply a column list. this is easily built from the data dictionary.




ops$tkyte%ORA11GR2> create table t1 ( x int, y int, z int, creation_date date, update_date date, "USER" varchar2(30) );

Table created.

ops$tkyte%ORA11GR2> create table t2 ( a int, b int, c int, creation_date date, update_date date, "USER" varchar2(30) );

Table created.

ops$tkyte%ORA11GR2> 
ops$tkyte%ORA11GR2> declare
  2      procedure p( p_fmt in varchar2, p_str in varchar2 default null )
  3      as
  4      begin
  5          dbms_output.put_line( replace( p_fmt, '%s', p_str ) );
  6      end;
  7  begin
  8      for x in ( select table_name, listagg(column_name, ',' ) within group (order by column_id) clist
  9                   from user_tab_columns
 10                  where column_name not in ('CREATION_DATE', 'UPDATE_DATE', 'USER' )
 11                  group by table_name )
 12      loop
 13          p( q'|dbms_comparison.create_comparison( |' );
 14          p( q'|comparison_name => '%s', |', x.table_name || '_cmp' );
 15          p( q'|schema_name => '%s', |', user );
 16          p( q'|object_name => '%s', |', x.table_name );
 17          p( q'|.... other stuff....|' );
 18          p( q'|column_list => '%s', |', x.clist );
 19          p( q'|.... other stuff....|' );
 20          p( q'|);|' );
 21      end loop;
 22  end;
 23  /
dbms_comparison.create_comparison(
comparison_name => 'T1_cmp',
schema_name => 'OPS$TKYTE',
object_name => 'T1',
.... other stuff....
column_list => 'X,Y,Z',
.... other stuff....
);
dbms_comparison.create_comparison(
comparison_name => 'T2_cmp',
schema_name => 'OPS$TKYTE',
object_name => 'T2',
.... other stuff....
column_list => 'A,B,C',
.... other stuff....
);

PL/SQL procedure successfully completed.


Rating

  (3 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Lakshmi Santhanam, February 26, 2013 - 9:41 am UTC

Thank you Tom. This is a great feature inside 11g.

dbms_comparision on 11.2.0.3 ?

Rajeshwaran, Jeyabal, February 28, 2013 - 7:56 am UTC

Tom,

I don't find dbms_comparision on 11.2.0.2 is that a new API added in 11.2.0.3 ?
rajesh@ORA11G> select * from v$version;

BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
PL/SQL Release 11.2.0.2.0 - Production
CORE    11.2.0.2.0      Production
TNS for Linux: Version 11.2.0.2.0 - Production
NLSRTL Version 11.2.0.2.0 - Production

Elapsed: 00:00:00.85
rajesh@ORA11G> desc dbms_comparision;
ERROR:
ORA-04043: object dbms_comparision does not exist

Tom Kyte
February 28, 2013 - 8:06 am UTC

http://docs.oracle.com/cd/E11882_01/appdev.112/e16760/d_comparison.htm

it has been around for a while.


either you do not have execute on it, or your dba did not install it.

A reader, March 05, 2013 - 11:17 am UTC

You have a typo. The correct spelling is:

dbms_comparison;
Tom Kyte
March 05, 2013 - 1:16 pm UTC

ahhh ;) you are correct, thanks!

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library