CREATE trigger ttbapi_tabela_preco_b02i for tbapi_tabela_preco
active before insert position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'TAB.PRECO INCLUIDA:'||NEW.ID||' - '||NEW.DESCRICAO;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBAPI',NEW.API_ID, HISTORICO);
    END
end;

CREATE trigger ttbapi_tabela_preco_b01u for tbapi_tabela_preco
active before update position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

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

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

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

    IF (HISTORICO <> '') THEN
    BEGIN
        HISTORICO = ' TAB.PRECO '||NEW.ID|| ' ALTERADO: '||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBAPI',NEW.API_ID, HISTORICO);
    END
END;

CREATE trigger ttbapi_tabela_preco_b01d for tbapi_tabela_preco
active before delete position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'TAB.PRECO EXCLUIDA:'||OLD.ID||' - '||OLD.DESCRICAO;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBAPI',OLD.API_ID, HISTORICO);
    END
end;



