Subject Re: [IBO] How to get the record id that was just stored with a query
Author Helen Borrie
At 08:19 AM 30/06/2005 +0200, you wrote:
> >
> > a 3rd way is to ask for the ID in the AfterInsert event with
> > IB_Dataset.FieldByName('ID').AsInt64 := IB_Dataset.Gen_ID('GEN_name',10);
> >
> > Alan
> >
>This wouldn't necessarily be the value used for the insert when there
>are others inserting data into the same table at the same time.

Actually, the way it was written was a bit misleading. However, at the
After Insert event, the new record has not yet been posted.

What should be made clear is that, with Firebird and IB, you don't have to
wait until after the record has been posted to assign the value from a
generator. Both IBO's Gen_ID() function and GeneratorLinks get the
generator value *before* anything is written to the database. Don't use
both - use one or the other!! And simply DO NOT use any retrospective means
to try to determine the value. It is totally unreliable to do so in a
multi-transaction environment.

Alan's "third method" is not recommended for IBO. It is just too darned
easy to run the thing multiple times by accident.

If using GeneratorLinks or Gen_ID(), make sure the Before Insert trigger
for your table is safe, viz.

if (new.ID is null) then
new.ID = GEN_ID(YourGenerator, 1);

If you don't fix this, you will be getting two numbers generated every time
you do an insert, and will mess up the keys of any dependent records.