CREATE OR REPLACE
TRIGGER USR_ATU_GRUPFILI BEFORE INSERT OR DELETE OR UPDATE OF NMROTATRANS ON ROTATRANS
FOR EACH ROW 
DECLARE 
   V_NOVOCODIGO VARCHAR2(20);
   V_CONTADOR   NUMERIC; 
BEGIN
  IF UPDATING THEN
    UPDATE GRUPFILI
       SET NMGRUPFILI = :NEW.NMROTATRANS
     WHERE NMGRUPFILI = :OLD.NMROTATRANS;
  ELSE
    IF DELETING THEN
      DELETE GRUPFILI
       WHERE NMGRUPFILI = :OLD.NMROTATRANS;
    ELSE
      IF INSERTING THEN
        NOVO_CODIGO_SC('GRUPFILI',V_NOVOCODIGO);
        SELECT COUNT(1)
          INTO V_CONTADOR
          FROM GRUPFILI
         WHERE CDGRUPFILI = SUBSTR(V_NOVOCODIGO,11,10);
        IF V_CONTADOR = 0 THEN
           INSERT INTO GRUPFILI (CDGRUPFILI, NMGRUPFILI)
                         VALUES (SUBSTR(V_NOVOCODIGO,11,10), :NEW.NMROTATRANS);
        ELSE
           UPDATE GRUPFILI
              SET NMGRUPFILI = :NEW.NMROTATRANS
            WHERE CDGRUPFILI = SUBSTR(V_NOVOCODIGO,11,10);
        END IF;
      END IF;
    END IF;
  END IF;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('USR_ATU_GRUPFILI','0.00.000'); END;
/


