INSERT INTO TBMENU (DESCRICAO, CONTROLE, GRUPO, SUBGRUPO, TIPO, URL)
            VALUES ('CADASTRO DE OCORRÊNCIAS', 23070, 'RH', 'RH', '1', '');
    
CREATE trigger tTBRH_OCORRENCIA_b01i for TBRH_OCORRENCIA
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('TBRH_OCORRENCIA',NEW.id, HISTORICO);
    END
end;

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

    HISTORICO = '';

    IF ( OLD.OBSERVACAO <> NEW.OBSERVACAO) THEN
    BEGIN
        HISTORICO = HISTORICO||'Obs.:'|| OLD.OBSERVACAO||'->'||NEW.OBSERVACAO;
    END
	
	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.status <> NEW.status) THEN
    BEGIN
        HISTORICO = HISTORICO||', Status:'|| OLD.status||'->'||NEW.status;
    END
	
	IF ( OLD.PERMITE_OBSERVACAO <> NEW.PERMITE_OBSERVACAO) THEN
    BEGIN
        HISTORICO = HISTORICO||', Perm. Obs.:'|| OLD.PERMITE_OBSERVACAO||'->'||NEW.PERMITE_OBSERVACAO;
    END
	
	IF ( OLD.PERMITE_AVALIACAO <> NEW.PERMITE_AVALIACAO) THEN
    BEGIN
        HISTORICO = HISTORICO||', Perm. Aval.:'|| OLD.PERMITE_AVALIACAO||'->'||NEW.PERMITE_AVALIACAO;
    END
    
    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBRH_OCORRENCIA',NEW.id, HISTORICO);
    END
end;

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

CREATE SEQUENCE GTBRH_OCORRENCIA;

SET GENERATOR GTBRH_OCORRENCIA TO 3;


