If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > comparing tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-09-02, 03:18
wvanriel wvanriel is offline
Registered User
 
Join Date: Oct 2002
Posts: 2
Question comparing tables

My employer has asked me to compare two databases which are not connected (1 in Oracle 7, and 1 in oracle 8). but with more or less the same set of tables and records (let's say: custumors and addresses). Can anyone give me a hint how to compare those tables. I need to know which records in one table are missing in the other one? I need to know which records have the same unique identification but different other attributes, etc.
Reply With Quote
  #2 (permalink)  
Old 10-09-02, 08:25
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Re: comparing tables

First, you need to establish a database link from one database to the other, so that you can connect to one database and select data from the other, e.g.

DB1> SELECT * FROM scott.emp@DB2;

Your DBA should be able to help with this.

Then you can compare records. To see which records in DB1 are missing in DB2, run this in DB1:

SELECT keycol1, keycol2, ... FROM table1
MINUS
SELECT keycol1, keycol2, ... FROM table1@DB2;

To find records where keys match but other attributes differ:

SELECT t1.keycol1, t1.keycol2, ...
FROM table t1, table@DB2 t2
WHERE t1.keycol1 = t2.keycol1
AND t1.keycol2 = t2.keycol2
...
AND ( t1.attribute1 != t2.attribute1
OR t1..attribute2 != t2.attribute2
OR ...
);

BEWARE OF NULLS!!!

If attribute1 can be NULL, then you must allow for that. The correct way is:

NOT ( ( t1.attribute1 IS NULL AND t2.attribute1 IS NULL)
OR t1.attribute1 = t2.attribute1
)

A simpler way is:

NVL(t1.attribute1,'???') = NVL(t2.attribute1,'???')

... but you must be sure to pick a suitable value (e.g. '???') that is (a) valid for the datatype of attribute1 and (b) not a value that you will actually find in attribute1.
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On