Subject | Re: [firebird-support] trigger -> know changed fields? |
---|---|
Author | Milan Babuskov |
Post date | 2006-02-01T15:24:17Z |
Yves Glodt wrote:
if (new.x is null and old.x is not null or
new.x is not null and old.x is null or
new.x <> old.x) then
-- x has changed
You shouldn't do it manually - generate it. It should be simple: for each
table column, replace "x" in the above example with field name...
select 'if (new.' || rdb$field_name || ' is null and old.' ||
rdb$field_name || ' is not null or new.' || rdb$field_name ||
'is not null and old.' || rdb$field_name || ' is null or new.' ||
rdb$field_name || ' <> old.' || rdb$field_name || ') then'
from rdb$relation_fields
where rdb$relation_name = 'EMPLOYEE';
Ok, that could be a start. You can also trim field names (and cast to varchar)
so you don't get the spaces, and add newlines, BEGIN..END block, etc.
HTH
--
Milan Babuskov
http://njam.sourceforge.net
http://www.flamerobin.org
> I wonder if I fire a trigger on update, is there a possibility ofManually. You also have to take care of NULLs:
> knowing which fields have changed, or do I have to do that "manually" by
> comparing .old and .new ?
if (new.x is null and old.x is not null or
new.x is not null and old.x is null or
new.x <> old.x) then
-- x has changed
You shouldn't do it manually - generate it. It should be simple: for each
table column, replace "x" in the above example with field name...
select 'if (new.' || rdb$field_name || ' is null and old.' ||
rdb$field_name || ' is not null or new.' || rdb$field_name ||
'is not null and old.' || rdb$field_name || ' is null or new.' ||
rdb$field_name || ' <> old.' || rdb$field_name || ') then'
from rdb$relation_fields
where rdb$relation_name = 'EMPLOYEE';
Ok, that could be a start. You can also trim field names (and cast to varchar)
so you don't get the spaces, and add newlines, BEGIN..END block, etc.
HTH
--
Milan Babuskov
http://njam.sourceforge.net
http://www.flamerobin.org