CREATE TRIGGER TTBPOS_B02I FOR TBPOS
ACTIVE BEFORE INSERT POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Registro Incluido';
    END

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

CREATE TRIGGER TTBPOS_B01U FOR TBPOS
ACTIVE BEFORE UPDATE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( OLD.ID_EMPRESA <> NEW.ID_EMPRESA) THEN
    BEGIN
        HISTORICO = HISTORICO||'ID da Empresa:'|| OLD.ID_EMPRESA||'->'||NEW.ID_EMPRESA;
    END

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

    IF ( OLD.NUMERO_SERIE <> NEW.NUMERO_SERIE) THEN
    BEGIN
        HISTORICO = HISTORICO||',Numero de Serie:'|| OLD.NUMERO_SERIE||'->'||NEW.NUMERO_SERIE;
    END

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

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

CREATE TRIGGER TTBPOS_B01D FOR TBPOS
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Registro Excluido';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBPOS',old.ID, HISTORICO);
    END
end;
