CREATE OR ALTER TRIGGER TTBOPERACAO_B04I FOR TBOPERACAO
ACTIVE BEFORE INSERT POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

  HISTORICO = '';

  IF (NEW.CODIGO <> '' AND NEW.ID > 0) THEN
  BEGIN
    HISTORICO = HISTORICO || 'Registro incluido';
  END

  IF(coalesce((SELECT A.CODIGO FROM TBOPERACAO A WHERE A.CODIGO = NEW.CODIGO),'') <> '') THEN
  BEGIN
    EXCEPTION "..." 'Registro ja existente com o CODIGO informado! CODIGO: ' || NEW.CODIGO;
  END

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