INSERT INTO TBMENU (DESCRICAO, CONTROLE, GRUPO, SUBGRUPO, TIPO, URL)
            VALUES ('CADASTRO DE REP', 23080, 'RH', 'RH', '1', '');

ALTER TABLE TBREP ADD STATUS INTEGER;
    
update TBREP set status = 1;

CREATE trigger tTBREP_b01i for TBREP
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('TBREP',NEW.id, HISTORICO);
    END
end;

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

    HISTORICO = '';

    IF ( OLD.codigo <> NEW.codigo) THEN
    BEGIN
        HISTORICO = HISTORICO||'Código.:'|| OLD.codigo||'->'||NEW.codigo;
    END
	
	IF ( OLD.DESCRICAO <> NEW.DESCRICAO) THEN
    BEGIN
        HISTORICO = HISTORICO||', Desc.:'|| OLD.DESCRICAO||'->'||NEW.DESCRICAO;
    END
    
    IF ( OLD.CONSULTA <> NEW.CONSULTA) THEN
    BEGIN
        HISTORICO = HISTORICO||', Consulta:'|| OLD.CONSULTA||'->'||NEW.CONSULTA;
    END
	
	IF ( OLD.SEQ <> NEW.SEQ) THEN
    BEGIN
        HISTORICO = HISTORICO||', Seq.:'|| OLD.SEQ||'->'||NEW.SEQ;
    END
    
    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBREP',NEW.id, HISTORICO);
    END
end;

CREATE OR ALTER trigger tTBREP_b01d for TBREP
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('TBREP',old.id, HISTORICO);
    END
end;


