CREATE OR REPLACE TRIGGER ATUALIZA_PARTFILI
AFTER INSERT
ON PRSUBGRPROD FOR EACH ROW
BEGIN
   INSERT INTO PARTFILI
      (CDFILIAL, CDPRODUTO, CDTIPPART, NRSEQPART, CDFORNECED, CDFILISUBGR, CDGRUPPROD, CDSUBGRPROD)
   SELECT DISTINCT
      CDFILIAL, :NEW.CDPRODUTO, '4', '1', CDFORNECED, :NEW.CDFILISUBGR, :NEW.CDGRUPPROD, :NEW.CDSUBGRPROD
   FROM
      PARTFILI
   WHERE
      CDGRUPPROD = :NEW.CDGRUPPROD AND
      CDSUBGRPROD = :NEW.CDSUBGRPROD AND
      CDTIPPART = '4' AND
      NOT EXISTS(SELECT P.CDFILIAL
                 FROM PARTFILI P
                 WHERE
                    P.CDFILIAL = PARTFILI.CDFILIAL AND
                    P.CDPRODUTO = :NEW.CDPRODUTO AND
                    P.CDTIPPART = PARTFILI.CDTIPPART AND
                    P.NRSEQPART = PARTFILI.NRSEQPART AND
                    P.CDFORNECED = PARTFILI.CDFORNECED);
END;
/
