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

   FOR C_USR_ZW2 IN (
   SELECT V_NOVOCODIGO AS W2_IDCLI, W2_NOME, W2_PESSOA, W2_TIPO,
          W2_END, W2_BAIRRO, W2_CEP,
          W2_PAIS, W2_IBGE, W2_MUN, W2_EST,
          W2_TEL, W2_TELEX, W2_FAX, W2_CONTATO,
          W2_ENDCOB, W2_BAIRROC, W2_CEPC,
          W2_MUNC, W2_ESTC, W2_ENDENT, W2_BAIRROE,
          W2_CEPE,
          W2_MUNE, W2_ESTE, W2_CGC,
          W2_INSCR, W2_COND, W2_EMAIL, W2_FLATIVO, W2_TPEDI,
          W2_HRGRV, W2_DTGRV, W2_COMPLEM, W2_RG,
          W2_NREDUZ, W2_COD_MUN,
          W2_CCCRED, W2_CONTC
     FROM USR_ZW2
    WHERE W2_CGC = :OLD.NRINSJURCLIE
      AND W2_IDCLI = (SELECT MAX(W2_IDCLI) FROM USR_ZW2 WHERE W2_CGC = :OLD.NRINSJURCLIE)) LOOP

      INSERT INTO USR_ZW2
         (W2_IDCLI, W2_NOME, W2_PESSOA, W2_TIPO,
          W2_END, W2_BAIRRO, W2_CEP,
          W2_PAIS, W2_IBGE, W2_MUN, W2_EST,
          W2_TEL, W2_TELEX, W2_FAX, W2_CONTATO,
          W2_ENDCOB, W2_BAIRROC, W2_CEPC,
          W2_MUNC, W2_ESTC, W2_ENDENT, W2_BAIRROE,
          W2_CEPE,
          W2_MUNE, W2_ESTE, W2_CGC,
          W2_INSCR, W2_COND, W2_EMAIL, W2_FLATIVO, W2_TPEDI,
          W2_HRGRV, W2_DTGRV, W2_COMPLEM, W2_RG,
          W2_NREDUZ, W2_COD_MUN,
          W2_CCCRED, W2_CONTC)
         VALUES
         (C_USR_ZW2.W2_IDCLI, C_USR_ZW2.W2_NOME, C_USR_ZW2.W2_PESSOA, C_USR_ZW2.W2_TIPO,
          C_USR_ZW2.W2_END, C_USR_ZW2.W2_BAIRRO, C_USR_ZW2.W2_CEP,
          C_USR_ZW2.W2_PAIS, C_USR_ZW2.W2_IBGE, C_USR_ZW2.W2_MUN, C_USR_ZW2.W2_EST,
          C_USR_ZW2.W2_TEL, C_USR_ZW2.W2_TELEX, C_USR_ZW2.W2_FAX, C_USR_ZW2.W2_CONTATO,
          C_USR_ZW2.W2_ENDCOB, C_USR_ZW2.W2_BAIRROC, C_USR_ZW2.W2_CEPC,
          C_USR_ZW2.W2_MUNC, C_USR_ZW2.W2_ESTC, C_USR_ZW2.W2_ENDENT, C_USR_ZW2.W2_BAIRROE,
          C_USR_ZW2.W2_CEPE,
          C_USR_ZW2.W2_MUNE, C_USR_ZW2.W2_ESTE, C_USR_ZW2.W2_CGC,
          C_USR_ZW2.W2_INSCR, C_USR_ZW2.W2_COND, C_USR_ZW2.W2_EMAIL, C_USR_ZW2.W2_FLATIVO, '2',
          TO_CHAR(SYSDATE, 'HH24MI'), TRUNC(SYSDATE), C_USR_ZW2.W2_COMPLEM, C_USR_ZW2.W2_RG,
          C_USR_ZW2.W2_NREDUZ, C_USR_ZW2.W2_COD_MUN,
          C_USR_ZW2.W2_CCCRED, C_USR_ZW2.W2_CONTC);
   END LOOP;
END;
/

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