DECLARE
   V_COUNT           NUMBER;
BEGIN
   V_COUNT      := 0;

   FOR C_FORN IN (SELECT I.CDFORNECED
                    FROM PRODFORNXMLFIL I
                   WHERE NOT EXISTS (SELECT P.CDFORNECED
                                       FROM FORNECEDOR P
                                      WHERE P.CDFORNECED = I.CDFORNECED))
   LOOP
      DELETE FROM PRODFORNXMLFIL
       WHERE CDFORNECED = C_FORN.CDFORNECED;

      V_COUNT := V_COUNT + 1;

      IF V_COUNT > 250 THEN
         V_COUNT := 0;
         COMMIT;
      END IF;
   END LOOP;

   FOR C_PROD IN (SELECT I.CDPRODUTO
                    FROM PRODFORNXMLFIL I
                   WHERE NOT EXISTS (SELECT P.CDPRODUTO
                                       FROM PRODUTO P
                                      WHERE P.CDPRODUTO = I.CDPRODUTO))
   LOOP
      DELETE FROM PRODFORNXMLFIL
       WHERE CDPRODUTO = C_PROD.CDPRODUTO;

      V_COUNT := V_COUNT + 1;

      IF V_COUNT > 250 THEN
         V_COUNT := 0;
         COMMIT;
      END IF;
   END LOOP;
   COMMIT;
END;
/

CREATE INDEX CDP01018_FK ON PRODFORNXMLFIL (CDFILIAL ASC)
/

ALTER TABLE PRODFORNXMLFIL
    ADD CONSTRAINT FKCDP01018FILIAL FOREIGN KEY (CDFILIAL)
       REFERENCES FILIAL (CDFILIAL)
/

CREATE INDEX CDP01019_FK ON PRODFORNXMLFIL (CDPRODUTO ASC)
/

ALTER TABLE PRODFORNXMLFIL
    ADD CONSTRAINT FKCDP01019FORNECED FOREIGN KEY (CDFORNECED)
       REFERENCES FORNECEDOR (CDFORNECED)
/

CREATE INDEX CDP01020_FK ON PRODFORNXMLFIL (CDFORNECED ASC)
/

ALTER TABLE PRODFORNXMLFIL
    ADD CONSTRAINT FKCDP01020PRODUTO FOREIGN KEY (CDPRODUTO)
       REFERENCES PRODUTO (CDPRODUTO)
/
