Ok

En poursuivant votre navigation sur ce site, vous acceptez l'utilisation de cookies. Ces derniers assurent le bon fonctionnement de nos services. En savoir plus.

Comparer deux schémas Oracle

compare.sh

 

#!/bin/sh
# Exemple : sh compare.sh 'SCHEMA1' 'SCHEMA2' 'DEV'
# Modifier la valeur de ORACLE_SID si besoin
export ORACLE_SID=DEV
SCHEMA1=$1
SCHEMA2=$2
DBLINK=$3
echo "Schema1="$SCHEMA1" ; Schema2="$SCHEMA2"@"$DBLINK
sqlplus / as sysdba <<-_EOF
@COMP1.sql $SCHEMA1 $SCHEMA2 $DBLINK;
exit;
_EOF

 

COMP1.sql

-- Au prealable on a cree un repertoire pour les scripts generes :
-- CREATE DIRECTORY SCRIPTS_GENERES as '/export/home/oracle/SCRIPTS_GENERES/'
SET SERVEROUTPUT ON
SET VERIFY OFF
DECLARE
    CURSOR c1 IS
        select table1, table2 from
            (select table_name table1 from dba_tables where owner='&1') t1
            full outer join
            (select table_name table2 from dba_tables@&3 where owner='&2') t2
            on t1.table1=t2.table2
        where (table1 is null and table2 not like 'BIN$%') or (table2 is null and table1 not like 'BIN$%')
        order by table1, table2;
        
    CURSOR c2 IS
        select table1, column1, table2, column2 from
            (select table_name table1, column_name column1
            from dba_tab_columns
            where owner='&1'
            and table_name not in (select view_name from dba_views where owner='&1')) t1
            full outer join
            (select table_name table2, column_name column2 from dba_tab_columns@&3
            where owner='&2'
            and table_name not in (select view_name from dba_views@&3 where owner='&2')) t2
            on t1.table1=t2.table2 and t1.column1=t2.column2
        where (column1 is null and table2 not like 'BIN$%') or (column2 is null and table1 not like 'BIN$%')
        order by table1, table2, column1, column2;
        
    CURSOR c3 is
        select table1, column1, type1, length1, precision1, scale1, table2, column2, type2, length2, precision2, scale2
        from
            (select table_name table1, column_name column1, data_type type1, data_length length1, data_precision precision1, data_scale scale1
            from dba_tab_columns
            where owner='&1'
            and table_name not in (select view_name from dba_views where owner='&1')) t1
            full outer join
            (select table_name table2, column_name column2, data_type type2, data_length length2, data_precision precision2, data_scale scale2
            from dba_tab_columns@&3
            where owner='&2'
            and table_name not in (select view_name from dba_views@&3 where owner='&2')) t2
            on t1.table1=t2.table2 and t1.column1=t2.column2
        where type1<>type2
        or length1<>length2
        or precision1<>precision2
        or scale1<>scale2
        or (type1 is not null and precision1 is null and precision2 is not null)
        or (type2 is not null and precision2 is null and precision1 is not null)
        order by table1, table2, column1, column2;
        
    CURSOR c4 IS
        select table1, table2 from
            (select view_name table1 from dba_views where owner='&1') t1
            full outer join
            (select view_name table2 from dba_views@&3 where owner='&2') t2
            on t1.table1=t2.table2
        where (table1 is null and table2 not like 'BIN$%') or (table2 is null and table1 not like 'BIN$%')
        order by table1, table2;
        
    CURSOR c5 IS
        select v1.view_name table1, v1.text_length length1, v2.view_name table2, v2.text_length length2
        from dba_views v1
        full outer join dba_views@&3 v2
        on v1.owner=v2.owner and v1.view_name=v2.view_name
        where v1.text_length<>v2.text_length
        and v1.owner='&1'
        and v2.owner='&2';
        
    filesortie UTL_FILE.FILE_TYPE;
    table1 varchar2(50);
    table2 varchar2(50);
    column1 varchar2(50);
    column2 varchar2(50);
    type1 varchar2(50);
    type2 varchar2(50);
    length1 varchar2(10);
    length2 varchar2(10);
    precision1 varchar2(10);
    precision2 varchar2(10);
    scale1 varchar2(10);
    scale2 varchar2(10);
BEGIN
    filesortie:= UTL_FILE.FOPEN('SCRIPTS_GENERES', 'resultat_comparaison.csv', 'W', 10000);
        
    UTL_FILE.PUT_LINE(filesortie,'Schema 1;Schema 2');
    UTL_FILE.NEW_LINE(filesortie);
    
    UTL_FILE.PUT_LINE(filesortie,'Tables schema 1;Tables schema 2');
    OPEN c1;
    LOOP
        FETCH c1 INTO table1, table2;
        EXIT WHEN c1%NOTFOUND;
        IF table1 is not null THEN
            UTL_FILE.PUT_LINE(filesortie,table1||';_');
        ELSIF table2 is not null THEN
            UTL_FILE.PUT_LINE(filesortie,'_;'||table2);
        END IF;
    END LOOP;
    CLOSE c1;
    UTL_FILE.NEW_LINE(filesortie);
    
    UTL_FILE.PUT_LINE(filesortie,'Colonnes schema 1;Colonnes schema 2');
    OPEN c2;
    LOOP
        FETCH c2 INTO table1, column1, table2, column2;
        EXIT WHEN c2%NOTFOUND;
        IF column1 is not null THEN
            UTL_FILE.PUT_LINE(filesortie,table1||'('||column1||');_');
        ELSIF column2 is not null THEN
            UTL_FILE.PUT_LINE(filesortie,'_;'||table2||'('||column2||')');
        END IF;
    END LOOP;
    CLOSE c2;
    UTL_FILE.NEW_LINE(filesortie);
    
    UTL_FILE.PUT_LINE(filesortie,'Types colonnes schema 1;Types colonnes schema 2');
    OPEN c3;
    LOOP
        FETCH c3 INTO table1, column1, type1, length1, precision1, scale1, table2, column2, type2, length2, precision2, scale2;
        EXIT WHEN c3%NOTFOUND;
        UTL_FILE.PUT(filesortie,table1||'('||column1||') : '||type1);
        UTL_FILE.PUT(filesortie,'('||length1||'/'||precision1||'/'||scale1||')');
        UTL_FILE.PUT(filesortie,';');
        UTL_FILE.PUT(filesortie,table2||'('||column2||') : '||type2);
        UTL_FILE.PUT(filesortie,'('||length2||'/'||precision2||'/'||scale2||')');
        UTL_FILE.NEW_LINE(filesortie);
    END LOOP;
    CLOSE c3;
    UTL_FILE.NEW_LINE(filesortie);
    
    UTL_FILE.PUT_LINE(filesortie,'Vues schema 1;Vues schema 2');
    OPEN c4;
    LOOP
        FETCH c4 INTO table1, table2;
        EXIT WHEN c4%NOTFOUND;
        IF table1 is not null THEN
            UTL_FILE.PUT_LINE(filesortie,table1||';_');
        ELSIF table2 is not null THEN
            UTL_FILE.PUT_LINE(filesortie,'_;'||table2);
        END IF;
    END LOOP;
    CLOSE c4;
    UTL_FILE.NEW_LINE(filesortie);
    
    UTL_FILE.PUT_LINE(filesortie,'Longueur texte vues schema 1;Longueur texte vues schema 2');
    OPEN c5;
    LOOP
        FETCH c5 INTO table1, length1, table2, length2;
        EXIT WHEN c5%NOTFOUND;
        UTL_FILE.PUT_LINE(filesortie,table1||' : '||length1||';'||table2||' : '||length2);
    END LOOP;
    CLOSE c5;

    UTL_FILE.FCLOSE(filesortie);

    DBMS_OUTPUT.PUT_LINE('Fin de l''execution du script');    
    DBMS_OUTPUT.PUT_LINE('Le resultat de la comparaison a ete genere dans le dossier /export/home/oracle/SCRIPTS_GENERES/');
END;
/

Écrire un commentaire

Optionnel