in reply to need to look for data differences in same table in two database instances

I can think of a few ways of doing this -

Method 1 (row by row comparison, the code is not tested, just a general idea of how the code would look like)
#!/usr/bin/perl -w use strict; use DBI; use DBD::Oracle; my $db1 = DBI->connect("dbi:Oracle:server=XXXX;database=XXXX", "USER", +"PASSWD") or die "Failed to connect to the database!"; my $db2 = DBI->connect("dbi:Oracle:server=XXXX;database=XXXX", "USER", +"PASSWD") or die "Failed to connect to the database!"; my $table = "DUMMY"; # name of the table my $unique_id = "ID"; # unique id to identify the row my $sth1 = $db1->prepare( "select * from $table" ); my $sth2 = $db2->prepare( "select * from $table where $unique_id=?" ); $sth1->execute(); while (my $row = $sth1->fetchrow_hashref()) { my $found = 0; $sth2->execute( $row->{$unique_id} ); while (my $row2 = $sth2->fetchrow_hashref()) { # compare the row foreach (keys %$row) { if ($row{$_} ne $row2{$_}) { print "DIFF: (ID) $_\n"; break; } } } } $sth->finish; $dbh->disconnect;

Method 2 - DBA way

step 1 - copy both tables into a dummy database, name the tables dummy1 and dummy2

step 2 - copy dummy1 to new table dummy3

step 3 - SQL: delete from dummy1 where ID is found in dummy2

step 4 - delete from dummy3 where ID is found in dummy1

step 5 - delete from dummy2 where ID is found in dummy 3

So in the end, dummy3 holds records in both the original dummy1 and dummy2 tables, and dummy1, dummy2 hold records that do not reconcile.

Method 3 - pg way

Have to say that pg's method is much clever.

  • Comment on Re: need to look for data differences in same table in two database instances
  • Download Code

Replies are listed 'Best First'.
Re: Re: need to look for data differences in same table in two database instances
by Anonymous Monk on Jan 09, 2004 at 17:03 UTC
    Thank you all for the responses. I'm trying Roger's method 1. I'm getting Global symbol "%row" requires explicit package name at compare.pl line 242. Global symbol "%row2" requires explicit package name at compare.pl line 242. I think I can fix that if it was a plain old variable. Thank you
      sorry that would be this line from Rogers Method 1 if ($row{$_} ne $row2{$_}) {
        My mistake. ;-)

        Should be if ($row->{$_} ne $row2->{$_}) {