create or replace PROCEDURE P_MIGRACION_09_CONTADOR (p_f_migracion       IN      DATE) IS

    K_USU_MIGRADO           VARCHAR2(7) := 'MIGRADO';
    
    --Variables para tablas de claves
    v_count_ad7400          PLS_INTEGER := 0;
    v_count_ad7401          PLS_INTEGER := 0;
    v_count_ad7402          PLS_INTEGER := 0;
    v_count_ad7403          PLS_INTEGER := 0;
    v_count_ad7406          PLS_INTEGER := 0;
    v_count_ad7424          PLS_INTEGER := 0;
    v_count_ad7425          PLS_INTEGER := 0;
    v_count_ad7427          PLS_INTEGER := 0;
    
    --Variables para tablas de AD74A correspondientes a claves
    v_count_ad74a00t00      PLS_INTEGER := 0;
    v_count_ad74a01t00      PLS_INTEGER := 0;
    v_count_ad74a02t00      PLS_INTEGER := 0;
    v_count_ad74a03t00      PLS_INTEGER := 0;
    v_count_ad74a06t00      PLS_INTEGER := 0;
    v_count_ad74a24t00      PLS_INTEGER := 0;
    v_count_ad74a25t00      PLS_INTEGER := 0;
    v_count_ad74a27t00      PLS_INTEGER := 0;
    
    --Variables para tablas de AD74A correspondientes a tablas
    v_count_ad74a04t00      PLS_INTEGER := 0;
    v_count_ad74a05t00      PLS_INTEGER := 0;
    v_count_ad74a07t00      PLS_INTEGER := 0;
    v_count_ad74a08t00      PLS_INTEGER := 0;
    v_count_ad74a16t00      PLS_INTEGER := 0;
    v_count_ad74a21t00      PLS_INTEGER := 0;
    v_count_ad74a22t00      PLS_INTEGER := 0;
    v_count_ad74a23t00      PLS_INTEGER := 0;
    v_count_ad74a29t00      PLS_INTEGER := 0;
    v_count_ad74a32t00      PLS_INTEGER := 0;
    v_count_ad74a32t01      PLS_INTEGER := 0;
    v_count_ad74a52t00      PLS_INTEGER := 0;
    v_count_ad74a71t00      PLS_INTEGER := 0;
    v_count_ad74a79t00      PLS_INTEGER := 0;
    v_count_ad74a82t00      PLS_INTEGER := 0;
    v_count_ad74a85t00      PLS_INTEGER := 0;
    v_count_ad74a89t00      PLS_INTEGER := 0;
    v_count_ad74a90t00      PLS_INTEGER := 0;
    v_count_ad74a91t00      PLS_INTEGER := 0;
    v_count_ad74a94t00      PLS_INTEGER := 0;

    --Variables para tablas de AE01B correspondientes a tablas
    v_count_ae01b00t00      PLS_INTEGER := 0;
    v_count_ae01b01t00      PLS_INTEGER := 0;
    v_count_ae01b05t00      PLS_INTEGER := 0;
    
    --Variables auxiliares
    v_max_migracion         PLS_INTEGER;

BEGIN
        --Claves para tabla AD74A00T00
        SELECT COUNT(1) count_ad7400
        INTO   v_count_ad7400
        FROM   claves_ad7400
        WHERE  TRUNC(fecha_mig) = TRUNC(NVL(p_f_migracion, SYSDATE)); 
        
        DBMS_OUTPUT.PUT_LINE('count_ad7400-->' || v_count_ad7400);
        
        --Claves para tabla AD74A01T00
        SELECT COUNT(1) count_ad7401
        INTO   v_count_ad7401
        FROM   claves_ad7401
        WHERE  TRUNC(fecha_mig) = TRUNC(NVL(p_f_migracion, SYSDATE)); 
        
        DBMS_OUTPUT.PUT_LINE('count_ad7401-->' || v_count_ad7401);
        
        --Claves para tabla AD74A02T00
        SELECT COUNT(1) count_ad7402
        INTO   v_count_ad7402
        FROM   claves_ad7402
        WHERE  TRUNC(fecha_mig) = TRUNC(NVL(p_f_migracion, SYSDATE)); 
        
        DBMS_OUTPUT.PUT_LINE('count_ad7402-->' || v_count_ad7402);
        
        --Claves para tabla AD74A03T00
        SELECT COUNT(1) count_ad7403
        INTO   v_count_ad7403
        FROM   claves_ad7403
        WHERE  TRUNC(fecha_mig) = TRUNC(NVL(p_f_migracion, SYSDATE)); 
        
        DBMS_OUTPUT.PUT_LINE('count_ad7403-->' || v_count_ad7403);
        
        --Claves para tabla AD74A06T00
        SELECT COUNT(1) count_ad7406
        INTO   v_count_ad7406
        FROM   claves_ad7406
        WHERE  TRUNC(fecha_mig) = TRUNC(NVL(p_f_migracion, SYSDATE)); 
        
        DBMS_OUTPUT.PUT_LINE('count_ad7406-->' || v_count_ad7406);
        
        --Claves para tabla AD74A24T00
        SELECT COUNT(1) count_ad7424
        INTO   v_count_ad7424
        FROM   claves_ad7424
        WHERE  TRUNC(fecha_mig) = TRUNC(NVL(p_f_migracion, SYSDATE)); 
        
        DBMS_OUTPUT.PUT_LINE('count_ad7424-->' || v_count_ad7424);
        
        --Claves para tabla AD74A25T00
        SELECT COUNT(1) count_ad7425
        INTO   v_count_ad7425
        FROM   claves_ad7425
        WHERE  TRUNC(fecha_mig) = TRUNC(NVL(p_f_migracion, SYSDATE)); 
        
        DBMS_OUTPUT.PUT_LINE('count_ad7425-->' || v_count_ad7425);
        
        --Claves para tabla AD74A27T00
        SELECT COUNT(1) count_ad7427
        INTO   v_count_ad7427
        FROM   claves_ad7427
        WHERE  TRUNC(fecha_mig) = TRUNC(NVL(p_f_migracion, SYSDATE)); 
        
        DBMS_OUTPUT.PUT_LINE('count_ad7427-->' || v_count_ad7427);
        
        
        /***    TABLAS MIGRADAS  AD74A correspondientes a claves   ****/
        
        --Tabla AD74A00T00
        SELECT COUNT(1) count_ad74a00t00
        INTO   v_count_ad74a00t00
        FROM   ad74a00t00
        WHERE  usu_alta_00 = K_USU_MIGRADO
        AND    TRUNC(f_alta_00) = TRUNC(NVL(p_f_migracion, SYSDATE)); 
        
        DBMS_OUTPUT.PUT_LINE('count_ad74a00t00-->' || v_count_ad74a00t00);
        
        --Tabla AD74A01T00
        SELECT COUNT(1) count_ad74a01t00
        INTO   v_count_ad74a01t00
        FROM   ad74a01t00
        WHERE  usu_alta_01 = K_USU_MIGRADO
        AND    TRUNC(f_alta_01) = TRUNC(NVL(p_f_migracion, SYSDATE));
        
        DBMS_OUTPUT.PUT_LINE('count_ad74a01t00-->' || v_count_ad74a01t00);
        
        --Tabla AD74A02T00
        SELECT COUNT(1) count_ad74a02t00
        INTO   v_count_ad74a02t00
        FROM   ad74a02t00
        WHERE  usu_alta_02 = K_USU_MIGRADO
        AND    TRUNC(f_alta_02) = TRUNC(NVL(p_f_migracion, SYSDATE));
        
        DBMS_OUTPUT.PUT_LINE('count_ad74a02t00-->' || v_count_ad74a02t00);
        
        --Tabla AD74A03T00
        SELECT COUNT(1) count_ad74a03t00
        INTO   v_count_ad74a03t00
        FROM   ad74a03t00
        WHERE  usu_alta_03 = K_USU_MIGRADO
        AND    TRUNC(f_alta_03) = TRUNC(NVL(p_f_migracion, SYSDATE));
        
        DBMS_OUTPUT.PUT_LINE('count_ad74a03t00-->' || v_count_ad74a03t00);
        
         --Tabla AD74A06T00
        SELECT COUNT(1) count_ad74a06t00
        INTO   v_count_ad74a06t00
        FROM   ad74a06t00
        WHERE  usu_alta_06 = K_USU_MIGRADO
        AND    TRUNC(f_alta_06) = TRUNC(NVL(p_f_migracion, SYSDATE));
        
        DBMS_OUTPUT.PUT_LINE('count_ad74a06t00-->' || v_count_ad74a06t00);
        
        --Tabla AD74A24T00
        SELECT COUNT(1) count_ad74a24t00
        INTO   v_count_ad74a24t00
        FROM   ad74a24t00
        WHERE  usu_alta_24 = K_USU_MIGRADO
        AND    TRUNC(f_alta_24) = TRUNC(NVL(p_f_migracion, SYSDATE));
        
        DBMS_OUTPUT.PUT_LINE('count_ad74a24t00-->' || v_count_ad74a24t00);
        
        --Tabla AD74A25T00
        SELECT COUNT(1) count_ad74a25t00
        INTO   v_count_ad74a25t00
        FROM   ad74a25t00
        WHERE  usu_alta_25 = K_USU_MIGRADO
        AND    TRUNC(f_alta_25) = TRUNC(NVL(p_f_migracion, SYSDATE));
        
        DBMS_OUTPUT.PUT_LINE('count_ad74a25t00-->' || v_count_ad74a25t00);
        
        --Tabla AD74A27T00
        SELECT COUNT(1) count_ad74a27t00
        INTO   v_count_ad74a27t00
        FROM   ad74a27t00
        WHERE  usu_alta_27 = K_USU_MIGRADO
        AND    TRUNC(f_alta_27) = TRUNC(NVL(p_f_migracion, SYSDATE));
        
        DBMS_OUTPUT.PUT_LINE('count_ad74a27t00-->' || v_count_ad74a27t00);
        
        
        /***    TABLAS MIGRADAS  AD74A correspondientes a tablas   ****/
        
        --Tabla AD74A04T00
        SELECT COUNT(1) count_ad74a04t00
        INTO   v_count_ad74a04t00
        FROM   ad74a04t00
        WHERE  usu_alta_04 = K_USU_MIGRADO
        AND    TRUNC(f_alta_04) = TRUNC(NVL(p_f_migracion, SYSDATE));
        
        DBMS_OUTPUT.PUT_LINE('count_ad74a04t00-->' || v_count_ad74a04t00);
        
        --Tabla AD74A05T00
        SELECT COUNT(1) count_ad74a05t00
        INTO   v_count_ad74a05t00
        FROM   ad74a05t00
        WHERE  usu_alta_05 = K_USU_MIGRADO
        AND    TRUNC(f_alta_05) = TRUNC(NVL(p_f_migracion, SYSDATE));
        
        DBMS_OUTPUT.PUT_LINE('count_ad74a05t00-->' || v_count_ad74a05t00);
        
        --Tabla AD74A07T00
        SELECT COUNT(1) count_ad74a07t00
        INTO   v_count_ad74a07t00
        FROM   ad74a07t00
        WHERE  usu_alta_07 = K_USU_MIGRADO
        AND    TRUNC(f_alta_07) = TRUNC(NVL(p_f_migracion, SYSDATE));
        
        DBMS_OUTPUT.PUT_LINE('count_ad74a07t00-->' || v_count_ad74a07t00);
        
        --Tabla AD74A08T00
        SELECT COUNT(1) count_ad74a08t00
        INTO   v_count_ad74a08t00
        FROM   ad74a08t00
        WHERE  usu_alta_08 = K_USU_MIGRADO
        AND    TRUNC(f_alta_08) = TRUNC(NVL(p_f_migracion, SYSDATE));
        
        DBMS_OUTPUT.PUT_LINE('count_ad74a08t00-->' || v_count_ad74a08t00);
        
        --Tabla AD74A16T00
        SELECT COUNT(1) count_ad74a16t00
        INTO   v_count_ad74a16t00
        FROM   ad74a16t00
        WHERE  usu_alta_16 = K_USU_MIGRADO
        AND    TRUNC(f_alta_16) = TRUNC(NVL(p_f_migracion, SYSDATE));
        
        DBMS_OUTPUT.PUT_LINE('count_ad74a16t00-->' || v_count_ad74a16t00);
        
        --Tabla AD74A21T00
        SELECT COUNT(1) count_ad74a21t00
        INTO   v_count_ad74a21t00
        FROM   ad74a21t00
        WHERE  usu_alta_21 = K_USU_MIGRADO
        AND    TRUNC(f_alta_21) = TRUNC(NVL(p_f_migracion, SYSDATE));
        
        DBMS_OUTPUT.PUT_LINE('count_ad74a21t00-->' || v_count_ad74a21t00);
        
        --Tabla AD74A22T00
        SELECT COUNT(1) count_ad74a22t00
        INTO   v_count_ad74a22t00
        FROM   ad74a22t00
        WHERE  usu_alta_22 = K_USU_MIGRADO
        AND    TRUNC(f_alta_22) = TRUNC(NVL(p_f_migracion, SYSDATE));
        
        DBMS_OUTPUT.PUT_LINE('count_ad74a22t00-->' || v_count_ad74a22t00);
        
        --Tabla AD74A23T00
        SELECT COUNT(1) count_ad74a23t00
        INTO   v_count_ad74a23t00
        FROM   ad74a23t00
        WHERE  usu_alta_23 = K_USU_MIGRADO
        AND    TRUNC(f_alta_23) = TRUNC(NVL(p_f_migracion, SYSDATE));
        
        DBMS_OUTPUT.PUT_LINE('count_ad74a23t00-->' || v_count_ad74a23t00);
        
        --Tabla AD74A29T00
        SELECT COUNT(1) count_ad74a29t00
        INTO   v_count_ad74a29t00
        FROM   ad74a29t00
        WHERE  usu_alta_29 = K_USU_MIGRADO
        AND    TRUNC(f_alta_29) = TRUNC(NVL(p_f_migracion, SYSDATE));
        
        DBMS_OUTPUT.PUT_LINE('count_ad74a29t00-->' || v_count_ad74a29t00);
        
        --Tabla AD74A32T00
        SELECT COUNT(1) count_ad74a32t00
        INTO   v_count_ad74a32t00
        FROM   ad74a32t00
        WHERE  usu_alta_32 = K_USU_MIGRADO
        AND    TRUNC(f_alta_32) = TRUNC(NVL(p_f_migracion, SYSDATE));
        
        DBMS_OUTPUT.PUT_LINE('count_ad74a32t00-->' || v_count_ad74a32t00);
        
        --Tabla AD74A32T01
        SELECT COUNT(1) count_ad74a32t01
        INTO   v_count_ad74a32t01
        FROM   ad74a32t01
        WHERE  usu_alta_32 = K_USU_MIGRADO
        AND    TRUNC(f_alta_32) = TRUNC(NVL(p_f_migracion, SYSDATE));
        
        DBMS_OUTPUT.PUT_LINE('count_ad74a32t01-->' || v_count_ad74a32t01);
        
        --Tabla AD74A52T00
        SELECT COUNT(1) count_ad74a52t00
        INTO   v_count_ad74a52t00
        FROM   ad74a52t00
        WHERE  usu_alta_52 = K_USU_MIGRADO
        AND    TRUNC(f_alta_52) = TRUNC(NVL(p_f_migracion, SYSDATE));
        
        DBMS_OUTPUT.PUT_LINE('count_ad74a52t00-->' || v_count_ad74a52t00);
        
        --Tabla AD74A71T00
        SELECT COUNT(1) count_ad74a71t00
        INTO   v_count_ad74a71t00
        FROM   ad74a71t00
        WHERE  usu_alta_71 = K_USU_MIGRADO
        AND    TRUNC(f_alta_71) = TRUNC(NVL(p_f_migracion, SYSDATE));
        
        DBMS_OUTPUT.PUT_LINE('count_ad74a71t00-->' || v_count_ad74a71t00);
        
        --Tabla AD74A79T00
        SELECT COUNT(1) count_ad74a79t00
        INTO   v_count_ad74a79t00
        FROM   ad74a79t00
        WHERE  usu_alta_79 = K_USU_MIGRADO
        AND    TRUNC(f_alta_79) = TRUNC(NVL(p_f_migracion, SYSDATE));
        
        DBMS_OUTPUT.PUT_LINE('count_ad74a79t00-->' || v_count_ad74a79t00);
        
        --Tabla AD74A82T00 NO TIENE CAMPO DE FECHA DE ALTA
        SELECT COUNT(1) count_ad74a82t00
        INTO   v_count_ad74a82t00
        FROM   ad74a82t00
        WHERE  usu_alta_82 = K_USU_MIGRADO
        AND    TRUNC(f_alta_82) = TRUNC(NVL(p_f_migracion, SYSDATE));
        
        DBMS_OUTPUT.PUT_LINE('count_ad74a82t00-->' || v_count_ad74a82t00);
        
        --Tabla AD74A85T00 NO TIENE CAMPO DE FECHA DE ALTA
        SELECT COUNT(1) count_ad74a85t00
        INTO   v_count_ad74a85t00
        FROM   ad74a85t00
        WHERE  usu_alta_85 = K_USU_MIGRADO
        AND    TRUNC(f_alta_85) = TRUNC(NVL(p_f_migracion, SYSDATE));
        
        DBMS_OUTPUT.PUT_LINE('count_ad74a85t00-->' || v_count_ad74a85t00);
        
        --Tabla AD74A89T00 
        SELECT COUNT(1) count_ad74a89t00
        INTO   v_count_ad74a89t00
        FROM   ad74a89t00
        WHERE  usu_alta_89 = K_USU_MIGRADO
        AND    TRUNC(f_alta_89) = TRUNC(NVL(p_f_migracion, SYSDATE));
        
        DBMS_OUTPUT.PUT_LINE('count_ad74a89t00-->' || v_count_ad74a89t00);
        
        /*
        --Tabla AD74A90T00 NO TIENE CAMPO DE FECHA DE ALTA Y NO ES NECESARIO CONTEO
        SELECT COUNT(1) count_ad74a90t00
        INTO   v_count_ad74a90t00
        FROM   ad74a90t00
        WHERE  usu_alta_90 = K_USU_MIGRADO
        AND    TRUNC(f_alta_90) = TRUNC(NVL(p_f_migracion, SYSDATE));
        
        DBMS_OUTPUT.PUT_LINE('count_ad74a90t00-->' || v_count_ad74a90t00);
        */
        
        --Tabla AD74A91T00 NO TIENE CAMPO DE FECHA DE ALTA
        SELECT COUNT(1) count_ad74a91t00
        INTO   v_count_ad74a91t00
        FROM   ad74a91t00
        WHERE  usu_alta_91 = K_USU_MIGRADO
        AND    TRUNC(f_alta_91) = TRUNC(NVL(p_f_migracion, SYSDATE));
        
        DBMS_OUTPUT.PUT_LINE('count_ad74a91t00-->' || v_count_ad74a91t00);
        
        --Tabla AD74A94T00 NO TIENE CAMPO DE FECHA DE ALTA
        SELECT COUNT(1) count_ad74a91t00
        INTO   v_count_ad74a94t00
        FROM   ad74a94t00
        WHERE  usu_alta_94 = K_USU_MIGRADO
        AND    TRUNC(f_alta_94) = TRUNC(NVL(p_f_migracion, SYSDATE));
        
        DBMS_OUTPUT.PUT_LINE('count_ad74a94t00-->' || v_count_ad74a94t00);
        
        /***    TABLAS MIGRADAS  AE01B correspondientes a tablas   ****/
        
        --Tabla AE01B00T00
        SELECT COUNT(1) count_ae01b00t00
        INTO   v_count_ae01b00t00
        FROM   ae01b.ae01b00t00
        WHERE  usu_alta_00 = K_USU_MIGRADO
        AND    TRUNC(f_alta_00) = TRUNC(NVL(p_f_migracion, SYSDATE));
        
        DBMS_OUTPUT.PUT_LINE('count_ae01b00t00-->' || v_count_ae01b00t00);
        
        --Tabla AE01B01T00
        SELECT COUNT(1) count_ae01b01t00
        INTO   v_count_ae01b01t00
        FROM   ae01b.ae01b01t00
        WHERE  usu_alta_01 = K_USU_MIGRADO
        AND    TRUNC(f_alta_01) = TRUNC(NVL(p_f_migracion, SYSDATE));
        
        DBMS_OUTPUT.PUT_LINE('count_ae01b01t00-->' || v_count_ae01b01t00);
        
         --Tabla AE01B05T00
        SELECT COUNT(1) count_ae01b05t00
        INTO   v_count_ae01b05t00
        FROM   ae01b.ae01b05t00
        WHERE  usu_alta_05 = K_USU_MIGRADO
        AND    TRUNC(f_alta_05) = TRUNC(NVL(p_f_migracion, SYSDATE));
        
        DBMS_OUTPUT.PUT_LINE('count_ae01b05t00-->' || v_count_ae01b05t00);
        
        
        /***    INSERT DE LS CONTEOS DE LAS TABLAS MIGRADAS     ****/
        
        --Primero se obtiene el máximo + 1 del ID_MIGRACION
        BEGIN
            SELECT NVL(MAX(id_migracion), 0) + 1
            INTO    v_max_migracion
            FROM    t_contadores_mig;
        EXCEPTION
            WHEN OTHERS THEN
                    RAISE_APPLICATION_ERROR(-20000, 'Error al obtener el máximo + 1 de la tabla T_CONTADORES_MIG');
        END;
        
        --Se insertan los valores obtenidos.
        BEGIN
            INSERT INTO t_contadores_mig (id_migracion,
                                          f_migracion,
                                          clave_ad7400,
                                          tabla_ad74a00t00,
                                          clave_ad7401,
                                          tabla_ad74a01t00,
                                          clave_ad7402,
                                          tabla_ad74a02t00,
                                          clave_ad7403,
                                          tabla_ad74a03t00,
                                          clave_ad7406,
                                          tabla_ad74a06t00,
                                          clave_ad7424,
                                          tabla_ad74a24t00,
                                          clave_ad7425,
                                          tabla_ad74a25t00,
                                          clave_ad7427,
                                          tabla_ad74a27t00,
                                          tabla_ad74a04t00,
                                          tabla_ad74a05t00,
                                          tabla_ad74a07t00,
                                          tabla_ad74a08t00,
                                          tabla_ad74a16t00,
                                          tabla_ad74a21t00,
                                          tabla_ad74a22t00,
                                          tabla_ad74a23t00,
                                          tabla_ad74a29t00,
                                          tabla_ad74a32t00,
                                          tabla_ad74a32t01,
                                          tabla_ad74a52t00,
                                          tabla_ad74a71t00,
                                          tabla_ad74a79t00,
                                          tabla_ad74a82t00,
                                          tabla_ad74a85t00,
                                          tabla_ad74a89t00,
                                          tabla_ad74a91t00,
                                          tabla_ad74a94t00,
                                          tabla_ae01b00t00,
                                          tabla_ae01b01t00,
                                          tabla_ae01b05t00
                                          )
                            VALUES       (v_max_migracion,
                                          NVL(p_f_migracion, SYSDATE),
                                          v_count_ad7400,
                                          v_count_ad74a00t00,
                                          v_count_ad7401,
                                          v_count_ad74a01t00,
                                          v_count_ad7402,
                                          v_count_ad74a02t00,
                                          v_count_ad7403,
                                          v_count_ad74a03t00,
                                          v_count_ad7406,
                                          v_count_ad74a06t00,
                                          v_count_ad7424,
                                          v_count_ad74a24t00,
                                          v_count_ad7425,
                                          v_count_ad74a25t00,
                                          v_count_ad7427,
                                          v_count_ad74a27t00,
                                          v_count_ad74a04t00,
                                          v_count_ad74a05t00,
                                          v_count_ad74a07t00,
                                          v_count_ad74a08t00,
                                          v_count_ad74a16t00,
                                          v_count_ad74a21t00,
                                          v_count_ad74a22t00,
                                          v_count_ad74a23t00,
                                          v_count_ad74a29t00,
                                          v_count_ad74a32t00,
                                          v_count_ad74a32t01,
                                          v_count_ad74a52t00,
                                          v_count_ad74a71t00,
                                          v_count_ad74a79t00,
                                          v_count_ad74a82t00,
                                          v_count_ad74a85t00,
                                          v_count_ad74a89t00,
                                          v_count_ad74a91t00,
                                          v_count_ad74a94t00,
                                          v_count_ae01b00t00,
                                          v_count_ae01b01t00,
                                          v_count_ae01b05t00
                                         );
        EXCEPTION
            WHEN OTHERS THEN
                RAISE_APPLICATION_ERROR(-20001, 'Error al insertar un registro de la tabla T_CONTADORES_MIG-->' || SQLCODE || ': ' || SQLERRM);
        END;  
        
        COMMIT;
END;
/