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

  HISTORICO = '';

  if (OLD.CLASSIFICACAO IS NULL AND NEW.CLASSIFICACAO IS NOT NULL) then
       BEGIN
       HISTORICO = HISTORICO||' MODELO INCLUIDO: '||NEW.CODIGO||' - '||NEW.DESCRICAO;
       END
       
  if (OLD.CLASSIFICACAO IS NOT NULL AND NEW.CLASSIFICACAO IS NOT NULL AND OLD.CLASSIFICACAO <> NEW.CLASSIFICACAO) then
       BEGIN
       HISTORICO = HISTORICO||' MODELO 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.CLASSIFICACAO AND P.TABELA = 'MOD'), HISTORICO);
  END
END;



CREATE OR ALTER trigger ttbmodelo_b11u for tbmodelo
active before update position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

  HISTORICO = '';

  if (OLD.CLASSIFICACAO IS NOT NULL AND NEW.CLASSIFICACAO = '') then
       BEGIN
       HISTORICO = HISTORICO||' MODELO 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.CLASSIFICACAO AND P.TABELA = 'MOD'), HISTORICO);
  END
END;
