CREATE OR ALTER trigger ttbup_fluxo_b02i for tbup_fluxo
active before insert position 2
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';

    IF ( NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' UP INCLUIDA: '||NEW.UP_ID||' - '||(SELECT FIRST 1 T.DESCRICAO FROM TBUP T WHERE T.ID = NEW.UP_ID);
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBFLUXO_PRODUCAO',NEW.FLUXO_ID, HISTORICO);
    END
END;


CREATE trigger tbup_fluxo_b02d for tbup_fluxo
active before delete position 2
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';

    IF ( OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' UP EXCLUIDA: '||OLD.UP_ID||' - '||(SELECT FIRST 1 T.DESCRICAO FROM TBUP T WHERE T.ID = OLD.UP_ID);
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBFLUXO_PRODUCAO',OLD.FLUXO_ID, HISTORICO);
    END
END;
