CREATE OR REPLACE PROCEDURE ATUALIZA_SUBGRUPFILIAL

AS

V_COUNT    NUMBER;
V_CURSOR_NAME INTEGER;
V_RET INTEGER;
V_FORENKEY VARCHAR2(2000);

BEGIN
   V_COUNT := 0;

   SELECT COUNT(*)
   INTO V_COUNT
   FROM SUBGRPRODFIL;

   IF V_COUNT = 0 THEN
      FOR C_SUBGRUPPROD IN (SELECT DISTINCT CDGRUPPROD, CDSUBGRPROD
                            FROM SUBGRUPPROD) LOOP

         INSERT INTO SUBGRPRODFIL
         (CDFILISUBGR, CDGRUPPROD, CDSUBGRPROD)
         VALUES
         ('P', C_SUBGRUPPROD.CDGRUPPROD, C_SUBGRUPPROD.CDSUBGRPROD);
      END LOOP;

      UPDATE PRSUBGRPROD
      SET CDFILISUBGR = 'P';

      UPDATE NVSUBGRPROD
      SET CDFILISUBGR = 'P';

   END IF;
END;
/

BEGIN
   ATUALIZA_SUBGRUPFILIAL;
END;
/

DROP PROCEDURE ATUALIZA_SUBGRUPFILIAL
/


