Creating a record within a trigger

whwar9739

Member
I am trying to accomplish something similar to the following:

Code:
def temp-table tt1 no-undo
   field field1 as char
   field field2 as dec
   field field3 as char.

on write of db-table new ndb-table old odb-table:
   run somescript.p().
   create tt1.
   assign
      tt1.field1 = something
      tt2.field2 = something
      tt3.field3 = something
   .
end. /* on write of db-table */

for each db-table:
   empty temp-table tt1.
   assign
      db-table.field = something
      db-table.field2 = something
   .

   run somescript2.p(input table tt1).

end. /* for each db-table */

The problem I am having, and maybe it is a progress 'feature', is that none of the temp-table records exist after leaving the trigger. I would just put the somescript2 in the trigger, however, I could have multiple tables and triggers creating records in tt1 and only need to run the somescript2 once. Any insight would be great.
 
Ok, I think I just figured out why my temp-table is empty.

The actual write trigger doesn't apply till the end of the for each db-table loop.

Is there a way to get the write to happen before the end of the for each loop?
 
Validate db-table.
or
release db-table.

right after changing db-table records

I do not think it is good programming method to fill temp-tables in triggers.
 
There are several reasons why you should not do this in a database trigger.


  1. You should not update the database or temp-tables from within triggers. One exception to that rule could be populating the record identifier in a create trigger. Because if you update other tables within the write trigger that have write triggers associated with them too you might end up in an infinity loop and your business logic is everything else but transparent - instead it is spread all over the place.
  2. The write database trigger fires on the end of the buffer or transaction scope, whichever comes first. Therefore it is not transparent in any case when the logic in the triggers is executed and you might experience funky behavior if you're not 100% positive 'bout what you're doing.
The reason why your temp-table is not populated at the end of the trigger is buffer scope. Since you did not define a defined buffer for the temp-table buffer you are using the default buffer associated with the temp-table and that buffer is scoped to the whole procedure. You could use the RELEASE statement in the trigger for your logic to work, but I would not advice that. Instead, if you really must, you should use a defined buffer ( defined in the trigger so that the scope is only the trigger block ).

HTH, RealHeavyDude.
 
Part of the reason for doing it in a trigger is that it seems simpler to do a buffer compare with old and new in the write trigger. Essentially I want to create a list of fields changed and store that in the temp-table. Unless any of you know how to compare the default buffer with the database values using buffer-compare.
 
OpenEdge has a command to compare buffers : BUFFER-COMPARE
it gives you the names of fields that differ, you can add code to find out field's values as well,
read ABL Reference Guide for details

BR, Mo
 
Back
Top