CREATE TRIGGER TTBCOR_B01I FOR TBCOR
ACTIVE BEFORE INSERT POSITION 0
as
begin
  if (new.CODIGO is null) then
    new.CODIGO = gen_id(GTBCOR,1);
end;

CREATE TRIGGER TTBCOR_B02I FOR TBCOR
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('TBCOR',NEW.CODIGO, HISTORICO);
    END
end;

CREATE TRIGGER TTBCOR_B01D FOR TBCOR
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( OLD.CODIGO > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Cor Excluida: '||old.CODIGO;
    END

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

