Subject Re: [firebird-support] timestamp trigger?
Author Thomas Steinmaurer
Hi,

> I need to have a timestamp that is updated every time a record is
> inserted or updated. I had asumed that just declaring the field as
> Timestamp this wold be acomplished but I was wrong. So a trigger
> (before update or insert) would be the next best thing. This is how I
> obtain the current date from firebird:
>
> select CURRENT_TIMESTAMP,
> from rdb$database;
>
> So I tried:
>
> select CURRENT_TIMESTAMP into NEW.MYTIMESTAMPFIELD,
> from rdb$database;
>
> did not work, so I tried:
>
> select CURRENT_TIMESTAMP into :NEW.MYTIMESTAMPFIELD,
> from rdb$database;

In PSQL:

NEW.MYTIMESTAMPFIELD = CURRENT_TIMESTAMP;


> Did not work either. So, can some one help me please? Is there another
> way to obtain an auto timestamp field?
>
> Can a BEFORE UPDATE and a BEFORE UPDATE OR INSERT coexist? Because my
> table already has a BEFORE INSERT trigger.

Sure.

With Firebird 1.5 you even can only have one trigger firing for both
events. But, I would declare your timestamp field with a DEFAULT
CURRENT_TIMESTAMP, so when inserting a new record the defined default
value will jump in, if you don't use the field in the INSERT INTO statement.

A BEFORE UPDATE with the assignment shown above, will update the
timestamp field whenever you update the record.


--
Best Regards,
Thomas Steinmaurer
LogManager Series - Logging/Auditing Suites supporting
InterBase, Firebird, Advantage Database, MS SQL Server and
NexusDB V2
Upscene Productions
http://www.upscene.com