CREATE trigger ttbesteira_b12u for tbesteira
active before update position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

  HISTORICO = '';

  if (OLD.PERFIL IS NULL AND NEW.PERFIL IS NOT NULL) then
       BEGIN
       HISTORICO = HISTORICO||' GP INCLUIDO: '||NEW.CODIGO||' - '||NEW.DESCRICAO;
       END
       
  if (OLD.PERFIL IS NOT NULL AND NEW.PERFIL IS NOT NULL AND OLD.PERFIL <> NEW.PERFIL) then
       BEGIN
       HISTORICO = HISTORICO||' GP INCLUIDO: '||NEW.CODIGO||' - '||NEW.DESCRICAO;
       END
  IF(HISTORICO <> '') THEN
  BEGIN
    EXECUTE PROCEDURE SPI_HISTORICO('TBPERFIL', (SELECT FIRST 1 P.CONTROLE FROM TBPERFIL P WHERE P.ID = NEW.PERFIL AND P.TABELA = 'GP'), HISTORICO);
  END
END;

CREATE trigger ttbesteira_b13u for tbesteira
active before update position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

  HISTORICO = '';

  if (OLD.PERFIL IS NOT NULL AND NEW.PERFIL = '') then
       BEGIN
       HISTORICO = HISTORICO||' GP EXCLUIDO: '||OLD.CODIGO||' - '||OLD.DESCRICAO;
       END

  IF(HISTORICO <> '') THEN
  BEGIN
    EXECUTE PROCEDURE SPI_HISTORICO('TBPERFIL', (SELECT FIRST 1 P.CONTROLE FROM TBPERFIL P WHERE P.ID = OLD.PERFIL AND P.TABELA = 'GP'), HISTORICO);
  END
END;
