SET TERM ^ ;

CREATE OR ALTER trigger ttbuf_b01u for tbuf
active before update position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';

    IF ( NEW.UF <> OLD.UF) THEN
    BEGIN
        HISTORICO = HISTORICO||' UF:'||OLD.UF||'->'||NEW.UF;
    END

    IF ( NEW.DESCRICAO IS DISTINCT FROM OLD.DESCRICAO) THEN
    BEGIN
        HISTORICO = HISTORICO||' DESC:'||COALESCE(OLD.DESCRICAO, '')||'->'||COALESCE(NEW.DESCRICAO, '');
    END

    IF ( NEW.REGIAO IS DISTINCT FROM OLD.REGIAO) THEN
    BEGIN
        HISTORICO = HISTORICO||' REGIAO:'||COALESCE(OLD.REGIAO, '')||'->'||COALESCE(NEW.REGIAO, '');
    END

    IF ( NEW.CODIGO_IBGE IS DISTINCT FROM OLD.CODIGO_IBGE) THEN
    BEGIN
        HISTORICO = HISTORICO||' COD.IBGE'||COALESCE(OLD.CODIGO_IBGE, '')||'->'||COALESCE(NEW.CODIGO_IBGE, '');
    END

    IF ( NEW.ALIQUOTA_ICMS IS DISTINCT FROM OLD.ALIQUOTA_ICMS) THEN
    BEGIN
        HISTORICO = HISTORICO||' ALIQ.INT.ICMS:'||CAST(COALESCE(OLD.ALIQUOTA_ICMS, 0) AS NUMERIC (15,2))||'->'||CAST(COALESCE(NEW.ALIQUOTA_ICMS, 0) AS NUMERIC (15,2));
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        HISTORICO = ' REGISTRO ALTERADO: '||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBUF',NEW.ID, HISTORICO);
    END
END
^

SET TERM ; ^

