-- INSERT
CREATE trigger tbmenu_b02i for tbmenu
active before insert position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

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

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMENU',NEW.id, HISTORICO);
    END
END;

-- UPDATE
CREATE trigger tbmenu_b01u for tbmenu
active before update position 7
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
	
	HISTORICO = '';

    IF ( OLD.controle <> NEW.controle) THEN
    BEGIN
        HISTORICO = HISTORICO||'Controle:'|| OLD.controle||'->'||NEW.controle;
    END

    IF (OLD.grupo <> NEW.grupo) THEN
    BEGIN
        HISTORICO = HISTORICO||'Grupo:'|| OLD.grupo||'->'||NEW.grupo;
    END

    IF (OLD.descricao <> NEW.descricao) THEN
    BEGIN
        HISTORICO = HISTORICO||'Desc.'|| OLD.descricao||'->'||NEW.descricao;
    END

    IF (OLD.url <> NEW.url) THEN
    BEGIN
        HISTORICO = HISTORICO||'URL: '|| OLD.url||'->'||NEW.url;
    END

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

-- Delete

CREATE trigger tbmenu_b01d for tbmenu
active before delete position 7
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

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

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBMENU',OLD.id, HISTORICO);
    END
END;