Wednesday, September 26, 2012

single value comparsion from three tables: Oracle SQL

ISSUE:
NEED alert for tablespace difference between 3 databases.

SOLUTION:

select a.tablespace_name,
NVL(prod.tablespace_name, '**MISSING**') DB1,
NVL(uat.tablespace_name,'**MISSING**') DB2,
NVL(upg.tablespace_name, '**MISSING**') DB3
from
(
(
select tablespace_name from dba_tablespaces@DB1
union
select tablespace_name from dba_tablespaces@DB2
union
select tablespace_name from dba_tablespaces@DB3
)
minus
(
 select tablespace_name from dba_tablespaces@DB1
 where tablespace_name in  (select tablespace_name from dba_tablespaces@DB2)
 and tablespace_name in (select tablespace_name from dba_tablespaces@DB3)
 )
) a ,
dba_tablespaces@DB1 db1,
dba_tablespaces@DB2 db2,
dba_tablespaces@DB3 db3
where a.tablespace_name=db1.tablespace_name(+)
and a.tablespace_name=db2.tablespace_name(+)
and  a.tablespace_name=db3.tablespace_name(+)
and a.tablespace_name not in ('EXAMPLE','UNDOTBS')
order by a.tablespace_name


OUTPUT:

TABLESPACE_NAME |DB1            |DB2          |DB3                                                                                     
----------------|---------------|-------------|---------------                                                                         
AAAAAA          |**MISSING**    |**MISSING**  |AAAAAA                                                                                  
AAAAAAAAAB      |AAAAAAAAAB     |**MISSING**  |AAAAAAAAAB                                                                              
AAAAAAAAATBS    |AAAAAAAAATBS   |**MISSING**  |AAAAAAAAATBS                                                                            
AAAAAAAAAAATBS  |**MISSING**    |**MISSING**  |AAAAAAAAAAATBS                                                                          
AAAAAAAABTBS    |**MISSING**    |AAAAAAAABTBS |AAAAAAAABTBS                                                                            
AAAAAAAABTB     |AAAAAAAABTB    |AAAAAAAABTB  |**MISSING**                                                                             
AAAAAAAABTC     |**MISSING**    |AAAAAAAABTC  |**MISSING**                                                                             
AAAAAAAACTC     |**MISSING**    |AAAAAAAACTC  |**MISSING**                                                                             
AAAAAAACCTC     |**MISSING**    |AAAAAAACCTC  |**MISSING**                                                                             
AAADAAACCTC     |AAADAAACCTC    |**MISSING**  |AAADAAACCTC                                                                             
AAAAACCTC       |AAAAACCTC      |**MISSING**  |AAAAACCTC                                                                               
AAAACCTC        |**MISSING**    |AAAACCTC     |**MISSING**                                                                             
AABBCCTC        |AABBCCTC       |**MISSING**  |**MISSING**                                                                             
AADDCCTC        |**MISSING**    |AADDCCTC     |**MISSING**                                                                             
AADDEETC        |**MISSING**    |AADDEETC     |**MISSING**                  


Comment:
this sql can be expended to compare multiple tables.

Reference:

http://www.techonthenet.com/sql/union.php

No comments:

Post a Comment