SET TERM ^ ;

CREATE OR ALTER trigger ttbcampanha_b01d for tbcampanha
active before delete position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
     HISTORICO = '';

     IF(OLD.ID > 0) THEN
     BEGIN
          HISTORICO = HISTORICO||'REGISTRO EXCLUIDO';

          DELETE FROM TBCAMPANHA_CLIENTES T
          WHERE T.CAMPANHA_ID = OLD.ID;

          DELETE FROM TBCAMPANHA_NOTIFICACAO N
          WHERE N.CAMPANHA_ID = OLD.ID;
     END

     IF (HISTORICO <> '') THEN
     BEGIN
          EXECUTE PROCEDURE SPI_HISTORICO('TBCAMPANHA',OLD.ID, HISTORICO);
     END
END
^

SET TERM ; ^

