CREATE OR REPLACE FUNCTION ARQDIGITAL_BLOCO9(
   P_CONTREGA     IN NUMBER,
   P_CONTREGB     IN NUMBER,
   P_CONTREGC     IN NUMBER,
   P_CONTREGD     IN NUMBER,
   P_CONTREGE     IN NUMBER,
   P_CONTREGH     IN NUMBER,
   P_NRSEQARQMAG  IN ARQMAGNETICO.NRSEQARQMAG%TYPE,
   P_CDLIVROMAG   IN ARQMAGNETICO.NRSEQARQMAG%TYPE) RETURN NUMBER
AS
   V_MOVIMENT     NUMBER;
   V_TOTREG9      NUMBER;
   V_CONTREG9     NUMBER;
   V_TOTREG       NUMBER;
   V_CONTREGA     NUMBER;
   V_CONT0450     NUMBER;
   V_CODIGO       VARCHAR2(100);
   V_ORIGEM       VARCHAR2(20);
   V_TEMFCP       VARCHAR2(10);
BEGIN
   V_CONTREGA := 0;
   FOR C_0150 IN (SELECT CDCHAVEREG, CDCAMPO1
                    FROM TMP_LVELETRONICO
                   WHERE CDREGISTRO = '0150'
                   ORDER BY CDCHAVEREG) LOOP
      IF C_0150.CDCAMPO1 = 'L' THEN
         FOR C_CLI IN (SELECT DISTINCT ('|0150'      || '|' ||
                                         CODIGO       || '|' ||
                                         NMRAZAO      || '|' ||
                                         CDPAIS       || '|' ||
                                         CNPJ1        || '|' ||
                                         CPF          || '|' ||
                                         CEI          || '|' ||
                                         NIT          || '|' ||
                                         ESTADO       || '|' ||
                                         INSCESTA     || '|' ||
                                         INSCESTST    || '|' ||
                                         MUNICIPIO    || '|' ||
                                         INSCMUNI     || '|' ||
                                         SUFRAMA      || '|' ) ARQ_0150,
                                         CNPJ
                               FROM(SELECT FI.CDFILIAL                                                  AS CODIGO,
                                           FI.NMFILIAL                                                  AS NMRAZAO,
                                           NVL(PA.CDPAISEXPFIS,'0'||PA.CDPAISBACEN)                     AS CDPAIS,
                                           FI.NRINSJURFILI                                              AS CNPJ1,
                                           ''                                                           AS CPF,
                                           ''                                                           AS CEI,
                                           ''                                                           AS NIT,
                                           DECODE(NVL(FI.IDENDEMISFIL,'P'),'I',IE.SGESTADO,EF.SGESTADO) AS ESTADO,
                                           FI.NRINSJURFILI                                              AS INSCESTA,
                                           ''                                                           AS INSCESTST,
                                           LPAD(NVL(M.CDMUNICIBGE,'0'),7,'0')                           AS MUNICIPIO,
                                           ''                                                           AS INSCMUNI,
                                           ''                                                           AS SUFRAMA,
                                           FI.CDFILIAL                                                  AS CNPJ
                                      FROM FILIAL FI, ENDEFILI EF, MUNICIPIO M, PAIS PA, INSCRESTAD IE
                                     WHERE FI.CDFILIAL    = C_0150.CDCHAVEREG
                                       AND EF.CDFILIAL(+) = FI.CDFILIAL
                                       AND NVL(EF.IDTPENDEFILI(+),'P') = NVL(FI.IDENDEMISFIL,'P')
                                       AND FI.CDEMPRESA   = IE.CDEMPRESA
                                       AND FI.CDINSCESTA  = IE.CDINSCESTA
                                       AND NVL(EF.CDPAIS,IE.CDPAIS) = M.CDPAIS
                                       AND NVL(EF.SGESTADO,IE.SGESTADO) = M.SGESTADO
                                       AND NVL(EF.CDMUNICIPIO,IE.CDMUNICIPIO) = M.CDMUNICIPIO
                                       AND NVL(EF.CDPAIS,IE.CDPAIS) = PA.CDPAIS)  )LOOP
            INSERT INTO ARQMAGNETICO
               (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
               ('06'||LPAD(RTRIM(V_CONTREGA), 8, '0'), C_CLI.ARQ_0150, P_NRSEQARQMAG);
            V_CONTREGA := NVL(V_CONTREGA,0) + 1;

            FOR C_REG0175 IN (SELECT DISTINCT ('|0175'                                                 || '|' ||
                                               REPLACE(RETIRA_CARACTER_ESPECIAL(EF.NRCEPFILI,'S'),'-') || '|' ||
                                               RETIRA_CARACTER_ESPECIAL(EF.DSENDEFILI,'S')             || '|' ||
                                               ''                                                      || '|' ||
                                               RETIRA_CARACTER_ESPECIAL(EF.DSCOMPENDFIL,'S')           || '|' ||
                                               RETIRA_CARACTER_ESPECIAL(EF.NMBAIRFILI,'S')             || '|' ||
                                               ''                                                      || '|' ||
                                               ''                                                      || '|' ||
                                               REPLACE(REPLACE(REPLACE(RETIRA_CARACTER_ESPECIAL(EF.NRTELEFILI,'S'),')'), '('), '-') || '|' ||
                                               REPLACE(REPLACE(REPLACE(RETIRA_CARACTER_ESPECIAL(EF.NRFAXFILI, 'S'),')'), '('), '-') || '|'  ) ARQ_0175
                                FROM FILIAL FI, ENDEFILI EF
                               WHERE FI.CDFILIAL    = C_0150.CDCHAVEREG
                                 AND EF.CDFILIAL(+) = FI.CDFILIAL
                                 AND NVL(EF.IDTPENDEFILI,'P') = NVL(FI.IDENDEMISFIL,'P')
                                 AND EF.SGESTADO = 'DF'
                                 AND ((EF.DSENDEFILI IS NOT NULL) OR (EF.NMBAIRFILI IS NOT NULL) OR (EF.NRCEPFILI  IS NOT NULL)) ) LOOP
               INSERT INTO ARQMAGNETICO
                 (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
               VALUES
                 ('06'||LPAD(RTRIM(V_CONTREGA), 8, '0'), C_REG0175.ARQ_0175, P_NRSEQARQMAG);
               V_CONTREGA := NVL(V_CONTREGA,0) + 1;
            END LOOP;
         END LOOP;
      ELSIF C_0150.CDCAMPO1 = 'C' THEN
         FOR C_REG0150 IN (SELECT DISTINCT ('|0150'      || '|' ||
                                            CODIGO       || '|' ||
                                            NMRAZAO      || '|' ||
                                            CDPAIS       || '|' ||
                                            CNPJ1        || '|' ||
                                            CPF          || '|' ||
                                            CEI          || '|' ||
                                            NIT          || '|' ||
                                            ESTADO       || '|' ||
                                            INSCESTA     || '|' ||
                                            INSCESTST    || '|' ||
                                            MUNICIPIO    || '|' ||
                                            INSCMUNI     || '|' ||
                                            SUFRAMA      || '|' ) ARQ_0150,
                                            CNPJ
                             FROM (SELECT CL.CDCLIENTE                                  AS CODIGO,
                                          CL.NMRAZSOCCLIE                               AS NMRAZAO,
                                          NVL(PA.CDPAISEXPFIS,'0'||PA.CDPAISBACEN)      AS CDPAIS,
                                          DECODE(CL.IDTPIJURCLIE, 'J', CL.NRINSJURCLIE) AS CNPJ1,
                                          DECODE(CL.IDTPIJURCLIE, 'F', CL.NRINSJURCLIE) AS CPF,
                                          ''                                            AS CEI,
                                          ''                                            AS NIT,
                                          EC.SGESTADO                                   AS ESTADO,
                                          CL.NRINSESTCLIE                               AS INSCESTA,
                                          ''                                            AS INSCESTST,
                                          LPAD(NVL(M.CDMUNICIBGE,'0'),7,'0')            AS MUNICIPIO,
                                          ''                                            AS INSCMUNI,
                                          ''                                            AS SUFRAMA,
                                          CL.CDCLIENTE AS CNPJ
                                     FROM CLIENTE CL, ENDECLIE EC, MUNICIPIO M, PAIS PA
                                    WHERE CL.CDCLIENTE       = C_0150.CDCHAVEREG
                                      AND EC.CDCLIENTE(+)    = CL.CDCLIENTE
                                      AND NVL(EC.IDTPENDECLIE,'P') = NVL(CL.IDENDEMISCLI,'P')
                                      AND EC.CDPAIS          = M.CDPAIS
                                      AND EC.SGESTADO        = M.SGESTADO
                                      AND EC.CDMUNICIPIO     = M.CDMUNICIPIO
                                      AND EC.CDPAIS          = PA.CDPAIS ) )LOOP
            INSERT INTO ARQMAGNETICO
              (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES
              ('06'||LPAD(RTRIM(V_CONTREGA), 8, '0'), C_REG0150.ARQ_0150, P_NRSEQARQMAG);
            V_CONTREGA := NVL(V_CONTREGA,0) + 1;

            FOR C_CLI0175 IN (SELECT DISTINCT ('|0175'                                                 || '|' ||
                                               REPLACE(RETIRA_CARACTER_ESPECIAL(EC.NRCEPCLIE,'S'),'-') || '|' ||
                                               RETIRA_CARACTER_ESPECIAL(EC.DSENDECLIE,'S')             || '|' ||
                                               ''                                                      || '|' ||
                                               RETIRA_CARACTER_ESPECIAL(EC.DSCOMPENDCLI,'S')           || '|' ||
                                               RETIRA_CARACTER_ESPECIAL(EC.NMBAIRCLIE,'S')             || '|' ||
                                               ''                                                      || '|' ||
                                               ''                                                      || '|' ||
                                               REPLACE(REPLACE(REPLACE(RETIRA_CARACTER_ESPECIAL(EC.NRTELECLIE,'S'),')'), '('), '-') || '|' ||
                                               REPLACE(REPLACE(REPLACE(RETIRA_CARACTER_ESPECIAL(EC.NRFAXCLIE, 'S'),')'), '('), '-') || '|'  ) ARQ_0175
                                FROM CLIENTE CL, ENDECLIE EC
                               WHERE CL.CDCLIENTE       = C_0150.CDCHAVEREG
                                 AND EC.CDCLIENTE(+)    = CL.CDCLIENTE
                                 AND NVL(EC.IDTPENDECLIE,'P') = NVL(CL.IDENDEMISCLI,'P')
                                 AND EC.SGESTADO = 'DF'
                                 AND ((EC.DSENDECLIE IS NOT NULL) OR (EC.NMBAIRCLIE IS NOT NULL) OR (EC.NRCEPCLIE IS NOT NULL)) ) LOOP
               INSERT INTO ARQMAGNETICO
                 (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
               VALUES
                 ('06'||LPAD(RTRIM(V_CONTREGA), 8, '0'), C_CLI0175.ARQ_0175, P_NRSEQARQMAG);
               V_CONTREGA := NVL(V_CONTREGA,0) + 1;
            END LOOP;
         END LOOP;
      ELSIF C_0150.CDCAMPO1 = 'F' THEN
         FOR C_FOR0150 IN (SELECT DISTINCT ('|0150'      || '|' ||
                                            CODIGO       || '|' ||
                                            NMRAZAO      || '|' ||
                                            CDPAIS       || '|' ||
                                            CNPJ1        || '|' ||
                                            CPF          || '|' ||
                                            CEI          || '|' ||
                                            NIT          || '|' ||
                                            ESTADO       || '|' ||
                                            INSCESTA     || '|' ||
                                            INSCESTST    || '|' ||
                                            MUNICIPIO    || '|' ||
                                            INSCMUNI     || '|' ||
                                            SUFRAMA      || '|' ) ARQ_0150,
                                            CNPJ
                             FROM (SELECT  FO.CDFORNECED                                    AS CODIGO,
                                           FO.NMRAZSOCFORN                                  AS NMRAZAO,
                                           NVL(PA.CDPAISEXPFIS,'0'||PA.CDPAISBACEN)         AS CDPAIS,
                                           DECODE(FO.IDTPIJURFORN, 'J', FO.NRINSJURFORN)    AS CNPJ1,
                                           DECODE(FO.IDTPIJURFORN, 'F', FO.NRINSJURFORN)    AS CPF,
                                           ''                                               AS CEI,
                                           ''                                               AS NIT,
                                           EF.SGESTADO                                      AS ESTADO,
                                           FO.NRINSESTFORN                                  AS INSCESTA,
                                           ''                                               AS INSCESTST,
                                           LPAD(NVL(M.CDMUNICIBGE,'0'),7,'0')               AS MUNICIPIO ,
                                           REPLACE(REPLACE(FO.NRINSMUNFORN,'-',''),'/','')  AS INSCMUNI,
                                           ''                                               AS SUFRAMA,
                                           FO.CDFORNECED AS CNPJ
                                      FROM FORNECEDOR FO, ENDEFORN EF, MUNICIPIO M, PAIS PA
                                     WHERE FO.CDFORNECED      = C_0150.CDCHAVEREG
                                       AND EF.CDFORNECED(+)   = FO.CDFORNECED
                                       AND NVL(EF.IDTPENDEFORN,'P') = NVL(FO.IDENDEMISFOR,'P')
                                       AND EF.CDPAIS          = M.CDPAIS
                                       AND EF.SGESTADO        = M.SGESTADO
                                       AND EF.CDMUNICIPIO     = M.CDMUNICIPIO
                                       AND EF.CDPAIS          = PA.CDPAIS)) LOOP
            INSERT INTO ARQMAGNETICO
              (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
            VALUES 
              ('06'||LPAD(RTRIM(V_CONTREGA), 8, '0'), C_FOR0150.ARQ_0150, P_NRSEQARQMAG);
            V_CONTREGA := NVL(V_CONTREGA,0) + 1;
            FOR C_REG0175 IN (SELECT DISTINCT ('|0175'                                                 || '|' ||
                                               REPLACE(RETIRA_CARACTER_ESPECIAL(EF.NRCEPFORN,'S'),'-') || '|' ||
                                               RETIRA_CARACTER_ESPECIAL(EF.DSENDEFORN,'S')             || '|' ||
                                               ''                                                      || '|' ||
                                               RETIRA_CARACTER_ESPECIAL(EF.DSCOMPENDFOR,'S')           || '|' ||
                                               RETIRA_CARACTER_ESPECIAL(EF.NMBAIRFORN,'S')             || '|' ||
                                               ''                                                      || '|' ||
                                               ''                                                      || '|' ||
                                               REPLACE(REPLACE(REPLACE(RETIRA_CARACTER_ESPECIAL(EF.NRTELEFORN,'S'),')'), '('), '-') || '|' ||
                                               REPLACE(REPLACE(REPLACE(RETIRA_CARACTER_ESPECIAL(EF.NRFAXFORN, 'S'),')'), '('), '-') || '|'  ) AS ARQ_0175
                                FROM FORNECEDOR FO, ENDEFORN EF
                               WHERE FO.CDFORNECED        = C_0150.CDCHAVEREG 
                                 AND EF.CDFORNECED(+)     = FO.CDFORNECED 
                                 AND NVL(EF.IDTPENDEFORN,'P')   = NVL(FO.IDENDEMISFOR,'P')
                                 AND EF.SGESTADO = 'DF' 
                                 AND ((EF.DSENDEFORN IS NOT NULL) OR (EF.NMBAIRFORN IS NOT NULL) OR (EF.NRCEPFORN IS NOT NULL))    ) LOOP 
               INSERT INTO ARQMAGNETICO 
                 (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
               VALUES 
                 ('06'||LPAD(RTRIM(V_CONTREGA), 8, '0'), C_REG0175.ARQ_0175, P_NRSEQARQMAG);
               V_CONTREGA := NVL(V_CONTREGA,0) + 1;
            END LOOP; 
         END LOOP; 
      END IF; 
   END LOOP; 
   FOR C_0200 IN (SELECT CDCHAVEREG, CDCAMPO1
                    FROM TMP_LVELETRONICO 
                   WHERE CDREGISTRO = '0200' 
                   ORDER BY CDCAMPO1) LOOP 
      FOR C_PROD IN (SELECT '|0200'     || '|' ||
                            P.CDARVPROD || '|' ||
                            P.NMPRODUTO || '|' ||
                            R.CDGENPROD || '|' ||
                            ''          || '|' AS ARQ_0200
                       FROM PRODUTO P, RELACPRODITGEN R
                      WHERE R.CDPRODUTO(+)  = P.CDPRODUTO
                        AND P.CDPRODUTO  = C_0200.CDCHAVEREG
                        AND R.CDLIVROMAG(+) = P_CDLIVROMAG
                      ORDER BY CDARVPROD) LOOP
         INSERT INTO ARQMAGNETICO
           (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
         VALUES
           ('07'||LPAD(RTRIM(V_CONTREGA), 8, '0'), C_PROD.ARQ_0200, P_NRSEQARQMAG);
         V_CONTREGA := NVL(V_CONTREGA,0) + 1;
      END LOOP;
   END LOOP;
   FOR C_REG0400 IN (SELECT DISTINCT('|0400'            || '|' ||
                                     CDFISCOPER         || '|' ||
                                     MAX(DSCDFISCOPER)  || '|'  ) ARQ_0400
                     FROM CODFISCOPER
                     GROUP BY CDFISCOPER ) LOOP
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        ('08'||LPAD(RTRIM(V_CONTREGA), 8, '0'), C_REG0400.ARQ_0400, P_NRSEQARQMAG);
      V_CONTREGA := NVL(V_CONTREGA,0) + 1;
   END LOOP;
   V_CONTREGA := NVL(V_CONTREGA,0) + 6;

   BEGIN
       SELECT MAX(SUBSTR(DSREGARQMAG,1,6)), COUNT(*)
         INTO V_TEMFCP, V_CONT0450
         FROM ARQMAGNETICO
        WHERE SUBSTR(DSREGARQMAG,1,6) = '|0450|'
          AND NRSEQARQMAG = P_NRSEQARQMAG
     GROUP BY SUBSTR(DSREGARQMAG,1,6);
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
           V_TEMFCP   := NULL;
           V_CONT0450 := NULL;
   END;

   IF V_TEMFCP = '|0450|' THEN    
      V_CONTREGA := NVL(V_CONTREGA,0) + NVL(V_CONT0450,0); 
   END IF; 

   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES
     ('10'||LPAD(RTRIM(V_CONTREGA), 8, '0'), '|0990'|| '|' ||V_CONTREGA|| '|' , P_NRSEQARQMAG);
   FOR REG_0 IN (SELECT SUBSTR(DSREGARQMAG,2,4) AS BLOCO, COUNT(*) AS QTDE
                   FROM ARQMAGNETICO
                  WHERE SUBSTR(DSREGARQMAG,2,1) = '0'
                    AND NRSEQARQMAG = P_NRSEQARQMAG
                  GROUP BY SUBSTR(DSREGARQMAG,2,4)
                  ORDER BY SUBSTR(DSREGARQMAG,2,4) ) LOOP
      V_CONTREG9 := NVL(V_CONTREG9,0) + 1;
      INSERT INTO ARQMAGNETICO
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
      VALUES
        ('62'||LPAD(RTRIM(V_CONTREG9), 8, '0'), '|9900'|| '|' ||REG_0.BLOCO||'|' ||REG_0.QTDE|| '|', P_NRSEQARQMAG);
   END LOOP;
   FOR REG_A IN (SELECT SUBSTR(DSREGARQMAG,2,4) AS BLOCO, COUNT(*) AS QTDE
                   FROM ARQMAGNETICO
                  WHERE SUBSTR(DSREGARQMAG,2,1) = 'A'
                    AND NRSEQARQMAG = P_NRSEQARQMAG 
                  GROUP BY SUBSTR(DSREGARQMAG,2,4) 
                  ORDER BY SUBSTR(DSREGARQMAG,2,4) ) LOOP 
      V_CONTREG9 := NVL(V_CONTREG9,0) + 1; 
      INSERT INTO ARQMAGNETICO 
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
      VALUES 
        ('62'||LPAD(RTRIM(V_CONTREG9), 8, '0'), '|9900'|| '|' ||REG_A.BLOCO||'|' ||REG_A.QTDE|| '|', P_NRSEQARQMAG);
   END LOOP; 
   FOR REG_B IN (SELECT SUBSTR(DSREGARQMAG,2,4) AS BLOCO, COUNT(*) AS QTDE 
                   FROM ARQMAGNETICO 
                  WHERE SUBSTR(DSREGARQMAG,2,1) = 'B' 
                    AND NRSEQARQMAG = P_NRSEQARQMAG 
                  GROUP BY SUBSTR(DSREGARQMAG,2,4) 
                  ORDER BY SUBSTR(DSREGARQMAG,2,4) ) LOOP 
      V_CONTREG9 := NVL(V_CONTREG9,0) + 1; 
      INSERT INTO ARQMAGNETICO 
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
      VALUES 
        ('62'||LPAD(RTRIM(V_CONTREG9), 8, '0'), '|9900'|| '|' ||REG_B.BLOCO||'|' ||REG_B.QTDE|| '|', P_NRSEQARQMAG); 
   END LOOP; 
   FOR REG_C IN (SELECT SUBSTR(DSREGARQMAG,2,4) AS BLOCO, COUNT(*) AS QTDE 
                   FROM ARQMAGNETICO 
                  WHERE SUBSTR(DSREGARQMAG,2,1) = 'C' 
                    AND NRSEQARQMAG = P_NRSEQARQMAG
                  GROUP BY SUBSTR(DSREGARQMAG,2,4) 
                  ORDER BY SUBSTR(DSREGARQMAG,2,4) ) LOOP 
      V_CONTREG9 := NVL(V_CONTREG9,0) + 1; 
      INSERT INTO ARQMAGNETICO 
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
      VALUES 
        ('62'||LPAD(RTRIM(V_CONTREG9), 8, '0'), '|9900'|| '|' ||REG_C.BLOCO||'|' ||REG_C.QTDE|| '|', P_NRSEQARQMAG); 
   END LOOP; 
   FOR REG_D IN (SELECT SUBSTR(DSREGARQMAG,2,4) AS BLOCO, COUNT(*) AS QTDE 
                   FROM ARQMAGNETICO 
                  WHERE SUBSTR(DSREGARQMAG,2,1) = 'D' 
                    AND NRSEQARQMAG = P_NRSEQARQMAG 
                  GROUP BY SUBSTR(DSREGARQMAG,2,4) 
                  ORDER BY SUBSTR(DSREGARQMAG,2,4) ) LOOP 
      V_CONTREG9 := NVL(V_CONTREG9,0) + 1; 
      INSERT INTO ARQMAGNETICO 
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
      VALUES 
        ('62'||LPAD(RTRIM(V_CONTREG9), 8, '0'), '|9900'|| '|' ||REG_D.BLOCO||'|' ||REG_D.QTDE|| '|', P_NRSEQARQMAG); 
   END LOOP; 
   FOR REG_E IN (SELECT SUBSTR(DSREGARQMAG,2,4) AS BLOCO, COUNT(*) AS QTDE 
                   FROM ARQMAGNETICO 
                  WHERE SUBSTR(DSREGARQMAG,2,1) = 'E' 
                    AND NRSEQARQMAG = P_NRSEQARQMAG 
                  GROUP BY SUBSTR(DSREGARQMAG,2,4) 
                  ORDER BY SUBSTR(DSREGARQMAG,2,4) ) LOOP 
      V_CONTREG9 := NVL(V_CONTREG9,0) + 1; 
      INSERT INTO ARQMAGNETICO 
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
      VALUES 
        ('62'||LPAD(RTRIM(V_CONTREG9), 8, '0'), '|9900'|| '|' ||REG_E.BLOCO||'|' ||REG_E.QTDE|| '|', P_NRSEQARQMAG); 
   END LOOP; 
   FOR REG_H IN (SELECT SUBSTR(DSREGARQMAG,2,4) AS BLOCO, COUNT(*) AS QTDE 
                   FROM ARQMAGNETICO 
                  WHERE SUBSTR(DSREGARQMAG,2,1) = 'H' 
                    AND NRSEQARQMAG = P_NRSEQARQMAG 
                  GROUP BY SUBSTR(DSREGARQMAG,2,4) 
                  ORDER BY SUBSTR(DSREGARQMAG,2,4) ) LOOP 
      V_CONTREG9 := NVL(V_CONTREG9,0) + 1; 
      INSERT INTO ARQMAGNETICO 
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
      VALUES 
        ('62'||LPAD(RTRIM(V_CONTREG9), 8, '0'), '|9900'|| '|' ||REG_H.BLOCO||'|' ||REG_H.QTDE|| '|', P_NRSEQARQMAG); 
   END LOOP; 
   FOR REG_8 IN (SELECT SUBSTR(DSREGARQMAG,2,4) AS BLOCO, COUNT(*) AS QTDE 
                   FROM ARQMAGNETICO 
                  WHERE SUBSTR(DSREGARQMAG,2,1) = '8' 
                    AND NRSEQARQMAG = P_NRSEQARQMAG 
                  GROUP BY SUBSTR(DSREGARQMAG,2,4) 
                  ORDER BY SUBSTR(DSREGARQMAG,2,4) ) LOOP 
      V_CONTREG9 := NVL(V_CONTREG9,0) + 1; 
      INSERT INTO ARQMAGNETICO 
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
      VALUES 
        ('62'||LPAD(RTRIM(V_CONTREG9), 8, '0'), '|9900'|| '|' ||REG_8.BLOCO||'|' ||REG_8.QTDE|| '|', P_NRSEQARQMAG); 
   END LOOP; 
   FOR REG_9 IN (SELECT SUBSTR(DSREGARQMAG,2,4) AS BLOCO, COUNT(*)+3 AS QTDE 
                   FROM ARQMAGNETICO 
                  WHERE SUBSTR(DSREGARQMAG,2,1) = '9' 
                    AND NRSEQARQMAG = P_NRSEQARQMAG 
                  GROUP BY SUBSTR(DSREGARQMAG,2,4) 
                  ORDER BY SUBSTR(DSREGARQMAG,2,4) ) LOOP 
      V_CONTREG9 := NVL(V_CONTREG9,0) + 1; 
      INSERT INTO ARQMAGNETICO 
        (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
      VALUES 
        ('62'||LPAD(RTRIM(V_CONTREG9), 8, '0'), '|9900'|| '|' ||REG_9.BLOCO||'|' ||REG_9.QTDE|| '|', P_NRSEQARQMAG); 
   END LOOP; 
   V_CONTREG9 := NVL(V_CONTREG9,0) + 2;
   INSERT INTO ARQMAGNETICO
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG) 
   VALUES 
     ('62'||LPAD(RTRIM(V_CONTREG9), 8, '0'), '|9900'|| '|' ||'9990'|| '|' ||'1'|| '|', P_NRSEQARQMAG); 
   IF V_CONTREG9 <> 0 THEN
      V_MOVIMENT := 0; 
   ELSE
      V_MOVIMENT := 1;
   END IF; 
   V_CONTREG9 := NVL(V_CONTREG9,0) + 1;
   INSERT INTO ARQMAGNETICO 
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES 
     ('60'||LPAD(RTRIM(V_CONTREG9), 8, '0'), '|9001'|| '|' ||V_MOVIMENT|| '|' , P_NRSEQARQMAG); 
   V_CONTREG9 := NVL(V_CONTREG9,0) + 1; 
   INSERT INTO ARQMAGNETICO 
     (NRORDREGARQ, DSREGARQMAG, NRSEQARQMAG)
   VALUES 
     ('63'||LPAD(RTRIM(V_CONTREG9), 8, '0'), '|9990'|| '|' ||V_CONTREG9|| '|', P_NRSEQARQMAG); 
   DELETE FROM TMP_LVELETRONICO; 
   RETURN(V_CONTREG9); 
END; 
/
-- VERSAO --
BEGIN ATUVEROBJ('ARQDIGITAL_BLOCO9','5.30.000'); END;
/

