0

For updating a field called 'last_modified' I'm trying to update this field automatically using a trigger, in stead of changing all update-statements. The field should only be updated when a field value has changed. Problem here is that you have to maintain the triggers if a table field is added, removed or renamed. The fields are stored in rdb$relation_fields, no problem. But building a query comparing the old and new value dynamically is.

create trigger test for test_table active before update position 0 as
declare variable fn char(31);
begin
  for select rdb$field_name from rdb$relation_fields where rdb$relation_name = 'test_table' into :fn       do
  begin
    if ('old.'||:fn <> 'new.'||:fn) then
    begin
      new.last_modified = current_timestamp;
      break;
    end
  end
end

Problem here is that 'old.'||:fn and 'new.':fn not really are comparing values, but literal strings, so the value of the fields cannot be compared. I've seen over here Firebird - get all modified fields inside a trigger that a trigger is attached to a system table, something I don't want to.

Is this fully-automated way of updating the 'last_modified' field not possible in this way? Or do I have to create a stored procedure that deletes al triggers and then recreates them with new fields, once I perform a update on the database (using this code http://www.firebirdfaq.org/faq133/).

Community
  • 1
  • 1
renevondecafe
  • 129
  • 1
  • 5
  • As far as I know you can't do this dynamically. You will need to rebuild the trigger when you add or remove columns. – Mark Rotteveel Nov 20 '14 at 10:28
  • I'd expected that, but I hoped for some nice features of Firebird. The workaround I have created is a stored procedure. This procedure removes all the triggers related to the last_modified column update. Then it recreates new triggers using the system tables: first find all tables having a last_modified, then create for each table a trigger generating a statement using the system tables again for retrieving the columns, to build the comparison-statement. Using this information the trigger can be created. It's an generic procedure which can be used abroad the database. – renevondecafe Nov 20 '14 at 14:42

0 Answers0