Triggers 在Firebird触发器中创建消息提示

Triggers 在Firebird触发器中创建消息提示,triggers,firebird,firebird2.5,Triggers,Firebird,Firebird2.5,是否可以在触发器中向用户提示错误消息 例如,如果用户没有在字段中输入值,则会提示用户,并且不会保存记录 我不知道错误消息是否是正确的术语。这可能是个例外。假设Firebird的版本至少为2.5,简短的回答是-是 您只需创建这样一个简单的异常 create exception my_universal_exception 'This is the default exception text'; 然后在触发器中使用它: ALTER TRIGGER A0 ACTIVE BEFORE insert

是否可以在触发器中向用户提示错误消息

例如,如果用户没有在字段中输入值,则会提示用户,并且不会保存记录


我不知道错误消息是否是正确的术语。这可能是个例外。

假设Firebird的版本至少为2.5,简短的回答是-是

您只需创建这样一个简单的异常

create exception my_universal_exception 'This is the default exception text';
然后在触发器中使用它:

ALTER TRIGGER A0 ACTIVE
BEFORE insert POSITION 0
as
begin
  if (new.id is null)
    then exception my_universal_exception 'You should specify some value for ID.';
end
就我个人而言,我正在使用一种更复杂的方法。我有一个专门的过程,它接受一个文本参数,默认情况下会引发异常,但是根据其他用户变量,它会将异常写入日志记录表或执行任何其他有用的操作:

CREATE PROCEDURE SHOW_ERROR (
    ERROR_TEXT varchar(256) )
AS
begin
  if ((select rdb$get_context('USER_SESSION', 'SILENT_MODE') from rdb$database) is not null)
    then insert into s_log(log_message)values(:error_text);
    else exception my_universal_exception :error_text;
end
因此,后来我在触发器中使用它,如下所示:

  if (new.id is null)
    then execute procedure show_error('You should specify some value for ID.');
另外请注意,从Firebird 3.0开始,您还可以在异常中使用参数


当然,您的客户端软件应该相应地处理此类错误,但这远远超出了这个问题的范围。

假设Firebird的版本至少为2.5,简短的回答是-是

您只需创建这样一个简单的异常

create exception my_universal_exception 'This is the default exception text';
然后在触发器中使用它:

ALTER TRIGGER A0 ACTIVE
BEFORE insert POSITION 0
as
begin
  if (new.id is null)
    then exception my_universal_exception 'You should specify some value for ID.';
end
就我个人而言,我正在使用一种更复杂的方法。我有一个专门的过程,它接受一个文本参数,默认情况下会引发异常,但是根据其他用户变量,它会将异常写入日志记录表或执行任何其他有用的操作:

CREATE PROCEDURE SHOW_ERROR (
    ERROR_TEXT varchar(256) )
AS
begin
  if ((select rdb$get_context('USER_SESSION', 'SILENT_MODE') from rdb$database) is not null)
    then insert into s_log(log_message)values(:error_text);
    else exception my_universal_exception :error_text;
end
因此,后来我在触发器中使用它,如下所示:

  if (new.id is null)
    then execute procedure show_error('You should specify some value for ID.');
另外请注意,从Firebird 3.0开始,您还可以在异常中使用参数

当然,您的客户端软件应该相应地处理此类错误,但这超出了这个问题的范围