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

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

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

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

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

    IF (HISTORICO <> '') THEN
    BEGIN
        HISTORICO = ' REG.ALTERADO: ETAPA: '||NEW.ID||' ALTERADA: '||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBESTEIRA',NEW.GP_ID, HISTORICO);
    END
END;