Link to home
Start Free TrialLog in
Avatar of bhanu823
bhanu823Flag for Afghanistan

asked on

compare two tables from different databases with diferent column names

Can any one help me out to compare the data between two tables where the tables are sitting on different databases with different columns

Ex:   'TB1' table in in 'DB1' sitting on server A,  with  'TB2' table in in 'DB2' sitting on server B
ASKER CERTIFIED SOLUTION
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bhanu823

ASKER

WHY DO WE NEED TO DO IT FOR  2 TIMES?.. NORMAL AND THEN REVERSE?
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

a Minus will also show 'missing' rows.  You need it twice for rows in one, not in the other and visa versa.
You say the column names are different, which is fine using minus, but you also need to be sure they are the same datatype and in the same order.
>>WHY DO WE NEED TO DO IT FOR  2 TIMES?..

Create a simple test case and see.    using the case below, if you only run it once you will not get the 'f' row.


drop table tab1 purge;
create table tab1(col1 char(1), col2 char(1));

drop table tab2 purge;
create table tab2(colz char(1), colx char(1));

insert into tab1 values('a','a');
insert into tab2 values('a','a');

insert into tab1 values('b','c');
insert into tab2 values('b','d');


insert into tab1 values('e','e');
insert into tab2 values('f','f');
commit;

select col1,col2 from tab1 minus select colz,colx from tab2;
select colz,colx from tab2 minus select col1,col2 from tab1;

Open in new window

If you want to issue one query -
select col1, col2, ... from tab1
union all
select colz, colx, ... from tab2
minus
(select col1, col2, ... from tab1
 intersect
 select colz, colx, ... from tab2)
>>If you want to issue one query -

If you do this you will likely need to add some designator to determine what rows came from what side.

For example, using the example above:  Does 'e' not exist in DB1 or DB2?