ALTER TABLE TBETIQUETAS
    ADD LINGUAGEM VARCHAR(10);

DROP TRIGGER TTBETIQUETAS_B0I;

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBETIQUETAS_B01I FOR TBETIQUETAS
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID IS NULL) THEN
    NEW.ID = GEN_ID(GTBETIQUETAS, 1);
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBETIQUETAS_B02I FOR TBETIQUETAS
ACTIVE BEFORE INSERT POSITION 1
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('TBETIQUETAS', NEW.ID, HISTORICO);
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBETIQUETAS_B01U FOR TBETIQUETAS
ACTIVE BEFORE UPDATE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(2000);
BEGIN

    HISTORICO = '';

    IF (OLD.DESCRICAO <> NEW.DESCRICAO) THEN
    BEGIN
        HISTORICO = HISTORICO||' DESC:'||OLD.DESCRICAO||'->'||NEW.DESCRICAO;
    END
    
    IF (OLD.TIPO <> NEW.TIPO) THEN
    BEGIN
        HISTORICO = HISTORICO||' TIPO:'||OLD.TIPO||'->'||NEW.TIPO;
    END
    
    IF (OLD.MULTIPLICADOR <> NEW.MULTIPLICADOR) THEN
    BEGIN
        HISTORICO = HISTORICO||' MULTI:'||OLD.MULTIPLICADOR||'->'||NEW.MULTIPLICADOR;
    END
    
    IF (OLD.LINGUAGEM <> NEW.LINGUAGEM) THEN
    BEGIN
        HISTORICO = HISTORICO||' LING:'||OLD.LINGUAGEM||'->'||NEW.LINGUAGEM;
    END
    
    IF (OLD.SCRIPT <> NEW.SCRIPT) THEN
    BEGIN
        HISTORICO = HISTORICO||' SCRIPT ALTERADO';
    END
    
    IF (HISTORICO <> '') THEN
    BEGIN
        HISTORICO = 'REGISTRO ALTERADO:'||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBETIQUETAS', OLD.ID, FN_ELIPSES(HISTORICO, 500));
    END
END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBETIQUETAS_B01D FOR TBETIQUETAS
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('TBETIQUETAS', OLD.ID, HISTORICO);
    END
END
^

SET TERM ; ^