create or replace PROCEDURE RECONCILIACAO_CNC(P_SEQESTABELEC IN NUMBER,
                                              P_DTINICIAL IN DATE,
                                              P_DTFINAL IN DATE)
AS
BEGIN
   FOR C_ESTABELEC IN
      (SELECT DISTINCT R.NRESTABELEC, MIN(R.DTVENDA) AS DTINICIAL, MAX(R.DTVENDA) AS DTFINAL
         FROM RETVENDAADQ R
        WHERE NRESTABELEC = NVL(P_SEQESTABELEC, NRESTABELEC)
          AND R.DTVENDA BETWEEN P_DTINICIAL AND P_DTFINAL
        GROUP BY R.NRESTABELEC) LOOP
      CONCILIACAO_CNC(C_ESTABELEC.NRESTABELEC, C_ESTABELEC.DTINICIAL, C_ESTABELEC.DTFINAL);
   END LOOP;
END;
/