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.


In reply to Re: need to look for data differences in same table in two database instances by Roger
in thread need to look for data differences in same table in two database instances by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.