Subject Re: [firebird-support] Rdb$db_key inside triggers
Author Ann W. Harrison
Nigel Weeks wrote:
> Is the db_key available inside triggers?

Err... In an after insert trigger, before or after update, and before
delete, I'd guess it would work. But, as it turns out, I'd be wrong.
Here's the output...


C:\harrison>isql
Use CONNECT or CREATE DATABASE to specify a database
SQL> create database 'foo.fdb';
SQL> create table t1 (f1 integer);
SQL> create table t2 (f1 integer, f2 char (8) character set octets, f3
char(3));
SQL> set term ^;
SQL> create trigger t1_bi for t1 before insert as begin
CON> insert into t2 (f1, f2, f3) values (new.f1, new.rdb$db_key, 'b/i');
CON> end^
SQL> create trigger t1_ai for t1 after insert as begin
CON> insert into t2 (f1, f2, f3) values (new.f1, new.rdb$db_key, 'a/i');
CON> end^
SQL> create trigger t1_bu for t1 before update as begin
CON> insert into t2 (f1, f2, f3) values (old.f1, new.rdb$db_key, 'b/u');
CON> end^
SQL> set term ;^
SQL> insert into t1 (f1) values (1);
SQL> commit;
SQL> insert into t1 (f1) values (2);
SQL> select * from t2;

F1 F2 F3
============ ================ ======

1 8000000001000000 b/i
1 8000000001000000 a/i
2 8000000001000000 b/i
2 8000000001000000 a/i

SQL> select rdb$db_key from t1;

DB_KEY
================

0000008000000001
0000008000000002


Note that the values for rdb$db_key are different in the log table and
the actual table...


> Is it ok to reference it via the following:
>
> IF(NEW.rdb$db_key IS NOT NULL)THEN
> blah blah

That, I'm much less clear about. I'm not at all sure that the NULL
state is handled "correctly" for db_keys. Actually, I'm quite sure that
doesn't work.


Regards,


Ann