SET TERM ^ ;

ALTER TABLE TBCOMPOSICAO
ADD OBSERVACAO VARCHAR(1000) CHARACTER SET UTF8 
DEFAULT '' 
COLLATE UTF8 ^

CREATE OR ALTER trigger ttbcomposicao_b01u for tbcomposicao
active before update position 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(2000);
BEGIN
    HISTORICO = '';
    IF(NEW.DESCRICAO <> OLD.DESCRICAO) THEN
    BEGIN
       HISTORICO = HISTORICO||' DESC: '||OLD.DESCRICAO||'->'||NEW.DESCRICAO;
    END
    
    IF(COALESCE(NEW.OBSERVACAO,'') <> COALESCE(OLD.OBSERVACAO,'')) THEN
    BEGIN
       HISTORICO = HISTORICO||' OBS: '||FN_ELIPSES(COALESCE(OLD.OBSERVACAO,''),200)||'->'||FN_ELIPSES(COALESCE(NEW.OBSERVACAO,''),200);
    END
    
    IF(HISTORICO <> '') THEN
    BEGIN
        HISTORICO = 'REGISTRO ALTERADO: '||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBCOMPOSICAO',NEW.ID,fn_elipse2(:HISTORICO,500));
    END
END^

SET TERM ; ^

