Skip to Main Content
  • Questions
  • Need script to compare table data of same table in different oracld databases

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Uma.

Asked: September 17, 2018 - 5:15 pm UTC

Last updated: September 19, 2018 - 12:56 am UTC

Version: 11

Viewed 10K+ times! This question is

You Asked

Hi,
I have a requirement where table1 exists in 2 diiferent oracle dbs . I need to write a shell script or pl sql block which would compare every column of the table in db 1 and db2 and give following output from unix. It should be able to take any two dbs and any table as input.
Output 1 : primary key is different . Which means so many records are say additional in the table in db2 on top of db1.
Output 2: for all those records in db1 which exist in db2 as well,that is same primary key, but some data in other columns is difference. In this case I need the exact column which has differen e
In both these scenarios, also I would need to convert the out records into insert stts.
Could you please help me with a script for this. I was checking dbms comparison package but I am nt able to see the actual column which has the difference anywhere in the views. Kindly guide
Example.
Table1 has primary key as say "id" and columns as c1 till c 10.
Now in Db1 ' Table1 has 100 records, Id1 to Id100.
In Db2, same table1 has 150'records . 50 additional records with primary key diff. ALSO in Db2, for the existing records from Id1 to Id100, some of the records' data is modified thatis data in any of the columns c1 to c10 are modified.
Now given input as Db1 , Db2 names, Table name as Table1, script should compare entire table data and give output as
Unique records in DB2 (as insert stts for entire record .. all column with values) say in a file, csv or seed sql. Similarly all records with difference with exact column which hs the diff.
Please help me with some sample code.

and Connor said...

Stew Ashton did a really nice series on this a while back. You can read it here

https://stewashton.wordpress.com/category/compare-and-sync/


Rating

  (3 ratings)

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

Comments

Thanks for the plug

Stew Ashton, September 19, 2018 - 8:37 am UTC

Uma already asked for help on my blog. He or she doesn't seem to understand that we are not free restaurants but cooking schools.

I'm sad because the recipes are there in my blog.

How are insert statements going to help for rows that already exist?

Anyway, just using the COMP_SYNC.CDC_SQL function will show all the changes. It is available here:
https://stewashton.wordpress.com/2018/02/12/comp_sync-1-a-new-table-compare-sync-package/

The output from that can be used in a MERGE statement to modify the target table. The MERGE can be generated from COMP_SYNC.SYNC_CDC.

Finally, my suggestion for actually seeing the differences can be applied to that same output: see
https://stewashton.wordpress.com/2018/05/21/actually-seeing-the-differences/




Forgot to add

Stew Ashton, September 19, 2018 - 8:40 am UTC

To Uma,

If you have any questions about my stuff, may I suggest you post them as comments on the appropriate pages of my blog?

When you post here I am not notified; when you comment on my blog I am notified.

So if you want an answer make sure I see the question ;)

Best regards,
Stew

compare table data of same table in different oracle databases

Mayur, September 21, 2018 - 11:36 am UTC

Hi..

First you need to Create DataBase link or DB Link between the Both Databases, and then you can compare with SQL Clause MINUS or NOT IN.

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