INSERT INTO TBMENU (DESCRICAO, CONTROLE, GRUPO, SUBGRUPO, TIPO, URL)
            VALUES ('CADASTRO DE BASES LEGAIS', 21060, 'FIS', 'FIS', '1   ', '');
    
update TBBASELEGAL set STATUSEXCLUSAO = 1;


CREATE trigger tTBBASELEGAL_b01i for TBBASELEGAL
active before insert position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

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

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

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

    HISTORICO = '';

    IF ( OLD.descricao <> NEW.descricao) THEN
    BEGIN
        HISTORICO = HISTORICO||'Descricao:'|| OLD.descricao||'->'||NEW.descricao;
    END
    
    IF ( OLD.status <> NEW.status) THEN
    BEGIN
        HISTORICO = HISTORICO||', Status:'|| OLD.status||'->'||NEW.status;
    END
    
    IF ( OLD.FISCO <> NEW.FISCO) THEN
    BEGIN
        HISTORICO = HISTORICO||', Fisco:'|| OLD.FISCO||'->'||NEW.FISCO;
    END
    
    IF ( OLD.BASELEGAL <> NEW.BASELEGAL) THEN
    BEGIN
        HISTORICO = HISTORICO||', Base Legal';
    END
    
    
    IF ( OLD.STATUSEXCLUSAO <> NEW.STATUSEXCLUSAO and new.STATUSEXCLUSAO = 0) THEN
    BEGIN
        HISTORICO = 'Registro Excluido';
    END
    
    IF ( OLD.STATUSEXCLUSAO <> NEW.STATUSEXCLUSAO and new.STATUSEXCLUSAO = 1) THEN
    BEGIN
        HISTORICO = 'Exclusão desfeita';
    END
    
    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBBASELEGAL',NEW.codigo, HISTORICO);
    END
end;

CREATE OR ALTER trigger tTBBASELEGAL_b01d for TBBASELEGAL
active before delete position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

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

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