CREATE OR REPLACE TRIGGER INSERE_VENDADICCLIE
AFTER INSERT ON PEDIDO FOR EACH ROW

DECLARE 
   V_IDUTGRFXVENDADIC VARCHAR2(01);
BEGIN
   SELECT MAX(IDUTGRFXVENDADIC)
     INTO V_IDUTGRFXVENDADIC
     FROM CLIENTE
    WHERE CDCLIENTE = :NEW.CDCLIENTE;

   IF (V_IDUTGRFXVENDADIC = 'S') THEN
      FOR C_VENDADICCLIE IN (SELECT CDVENDEDOR
                               FROM VENDADICCLIE
                              WHERE CDCLIENTE = :NEW.CDCLIENTE)
      LOOP
         INSERT INTO VENDPEDAUX (CDFILIAL, NRPEDIDO, CDVENDEDOR)
         VALUES (:NEW.CDFILIAL, :NEW.NRPEDIDO, C_VENDADICCLIE.CDVENDEDOR);

      END LOOP;
   END IF;
END;
/