DECLARE V_CTFLUXO NUMBER;
BEGIN
   FOR C_CT IN
   (SELECT
       ROWID,
       CDCLASSFINA, CDEMPRESA, CDFIMOVFLPAG, DTMOVFLUXO, IDMOVFLUXO, VRORIGPAG
    FROM CONTAPAGAR
    WHERE CDTPCTPAGAR = '000062') LOOP

      SELECT COUNT(*) INTO V_CTFLUXO
      FROM MOVFLUXO
      WHERE CDCLASSFINA = '110001'
        AND CDEMPRESA   = C_CT.CDEMPRESA
        AND CDFILIAL    = C_CT.CDFIMOVFLPAG
        AND DTMOVFLUXO  = C_CT.DTMOVFLUXO
        AND IDMOVFLUXO  = C_CT.IDMOVFLUXO;

      IF V_CTFLUXO = 0 THEN
         INSERT INTO MOVFLUXO
           (CDCLASSFINA, CDEMPRESA, CDFILIAL, DTMOVFLUXO, IDMOVFLUXO,
            VRENTMFLUXO, VRSAIMFLUXO)
         VALUES
           ('110001', C_CT.CDEMPRESA, C_CT.CDFIMOVFLPAG, C_CT.DTMOVFLUXO, C_CT.IDMOVFLUXO,
            0, C_CT.VRORIGPAG);
      END IF;

      UPDATE CONTAPAGAR
      SET CDCLASSFINA = '110001'
      WHERE ROWID = C_CT.ROWID;
   END LOOP;
END;
/
COMMIT
/
