views:

83

answers:

4

I have two similar tables in oracle in two different databases. For example : my table name is EMPLOYEE and primary key is employee id. The same table with same columns(say 50 columns are is avlbl in two databases and two databases are linked.

I want to compare these two tables column by column and find out which records are not matching. i want the specific column in each row in two tables that are not matching.

+6  A: 
select *
from 
(
( select * from TableInSchema1
  minus 
  select * from TableInSchema2)
union all
( select * from TableInSchema2
  minus
  select * from TableInSchema1)
)

should do the trick if you want to solve this with a query

mcabral
+1 You might also want to add a column to each query to indicate where the data came from. For example: "select 1 schema1Or2, TableInSchema1.* from TableInSchema1 minus...". Then at the end order by some values and that new column, for example "order by 2, 3, 4, 5, 1 desc". Then you'll (probably) get related rows next to each other, and it will be obvious what's different versus what's missing.
jonearles
+1  A: 

Try to use 3rd party tool, such as SQL Data Examiner which compares Oracle databases and shows you differences.

SQLDev
+1  A: 

As an alternative which saves from full scanning each table twice and also gives you an easy way to tell which table had more rows with a combination of values than the other:

SELECT col1
     , col2
     -- (include all columns that you want to compare)
     , COUNT(src1) CNT1
     , COUNT(src2) CNT2
  FROM (SELECT a.col1
             , a.col2
             -- (include all columns that you want to compare)
             , 1 src1
             , TO_NUMBER(NULL) src2
          FROM tab_a a
         UNION ALL
        SELECT b.col1
             , b.col2
             -- (include all columns that you want to compare)
             , TO_NUMBER(NULL) src1
             , 2 src2
          FROM tab_b b
       )
 GROUP BY col1
        , col2
HAVING COUNT(src1) <> COUNT(src2) -- only show the combinations that don't match

Credit goes here: http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:1417403971710

Patrick Marchand
+4  A: 

Red Gate Software has a dedicated tool for solving this problem. You can download and try out the EAP of Data Compare for Oracle for free - http://www.red-gate.com/products/data_compare_for_oracle_eap/index.htm

Kind regards, Tom

Red Gate Software

Tom Harris