简体   繁体   中英

Comparing two tables from separate databases in DB2

I have a requirement where I want to reconcile data between tables which are in different databases in DB2 and out put the differences as a report in java.

The approach I am thinking of is:

  • Read first table in a Java Map Collection
  • Read second table in a Java Map Collection
  • Compare the two maps
  • Output the difference in a report

Is there any other efficient way to do this through java and sql ?

The tables will have around 10 to 15 columns and around 20000 rows of data.

Is my approach correct ?

PS: Things like federating one table into the other database etc. are not allowed.

If the table is too big, you could fill the memory. This is not a good thing.

What you can do is a Java Stored Procedure that use two connections (one where the SP is defined, and the other is a remote connection) You can compare the two tables by "simulating" the Zig Zag join. First, you sort all the columns (sort by 1, 2, 4, ...), then, you take the first row of the table in one database, and compares it with the first row of the table of the other database. If both are the same, you move forward both. If they are not the same, you move forward the lower value until you arrives to the same of the other table. You present the differences to the user, and you can also give the similarities.

For example, table with two columns (c1 char(1), c2 int)

DB1                 DB2
Table 1 | Zig zag | Table 1
c1 | c2 |    |    | c1 | c2   
a    1    1      1  a    1
b    2    2      2  c    2
                 3  d    3
e    3    3
f    4    4      4  f    4
f    5    5      5  f    6
f    7    6
                 6  f    8

Diffs

DB1 Table 1
c1 | c2
b    2
e    3
f    5
f    7

DB2 Table 1
c1 | c2
c    2
d    3
f    6
f    8

Similarities
2 rows
c1 | c2
a    1
f    4

Then, your stored procedure could return 3 result sets, indicating the difference for each table, and the similarities. BTW, you can do that also in pure SQL-PL or PL/SQL, if you could federate both DBs (I do not see a reason to not do that)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM