CREATE OR REPLACE TRIGGER USR_EXCLUSAO_PRODUTO
AFTER DELETE ON PRODUTO FOR EACH ROW
DECLARE
  V_NOVOCODIGO    INTEGER;
BEGIN
   /* Retorna o sequencial para a tabela */
   BEGIN
      SELECT W1_IDPROD.NEXTVAL
        INTO V_NOVOCODIGO
        FROM DUAL;
   EXCEPTION
   WHEN NO_DATA_FOUND THEN
      V_NOVOCODIGO := 1;
   END;

   FOR C_USR_ZW1 IN (
   SELECT V_NOVOCODIGO AS W1_IDPROD, W1_COD, W1_DESC, W1_UM,
          W1_DTGRV, W1_HRGRV, W1_TPEDI, W1_TIPO
     FROM USR_ZW1
    WHERE W1_COD = :OLD.CDARVPROD
      AND W1_IDPROD = (SELECT MAX(W1_IDPROD) FROM USR_ZW1 WHERE W1_COD = :OLD.CDARVPROD)) LOOP

      INSERT INTO USR_ZW1
      (W1_IDPROD, W1_COD, W1_DESC, W1_UM,
       W1_DTGRV, W1_HRGRV, W1_TPEDI, W1_TIPO)
       VALUES
      (C_USR_ZW1.W1_IDPROD, C_USR_ZW1.W1_COD, C_USR_ZW1.W1_DESC, C_USR_ZW1.W1_UM,
       TRUNC(SYSDATE), TO_CHAR(SYSDATE, 'HH24MI'), '2', C_USR_ZW1.W1_TIPO);
   END LOOP;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('USR_EXCLUSAO_PRODUTO','5.09.000'); END;
/
