问题描述
我不知道错误消息是否正确。可能是例外。
解决方法
假设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开始,您还可以在异常as described here中使用参数。
当然,您的客户端软件应相应地处理此类错误,但这超出了此问题的范围。