Reputation: 195
Is it possible in a trigger to prompt the user with an error message?
For example, if the user hadn't entered a value in a field, then it prompts them and doesn't save the record.
I don't know if error message is the correct term. It may be exception.
Upvotes: 0
Views: 524
Reputation: 437
Assuming Firebird's version is at least 2.5, the short answer is - yes.
You just create a simple exception like this
create exception my_universal_exception 'This is the default exception text';
Then use it in your trigger:
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
Personally I'm using a bit more complex approach. I have a dedicated procedure that takes a text argument and by default raises an exception however depending on other user variables writes the exception to a logging table or does any other useful things:
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
Thus later I use it in my triggers like this:
if (new.id is null)
then execute procedure show_error('You should specify some value for ID.');
Also please note, that starting from Firebird 3.0 you can also use parameters in your exceptions as described here.
And of course, your client software should handle such kind of error accordingly but this is way out of scope for this question.
Upvotes: 2