Subject Re: [ib-support] Re: Getting id of last inserted record
Author Lucas Franzen
David,


> > To have a reliable access to the id of last inserted record you
> have no
> > other option but to get the generator value on the client side.
> >
> > Depending on the tools you use there are different ways to do it
> (but
> > you can always do it the way you did above).

Sorry, I wasn't clear enough.

SELECT GEN_ID(GEN_ATABLE, 0) FROM RDB$DATABASE;

will return the latest inserted generator value - but it isn't reliable
since you can't be sure it was YOU who did the last call to the
generator...

But you can generate the next value on the client side, by setting the
increment to 1 (instead of 0):

SELECT GEN_ID(GEN_ATABLE, 1) FROM RDB$DATABASE;

will get you the next generator value.

So you have access to it on the client side.



>
> > To keep your db operatable outside the client-app you can supply a
> value
> > in a before insert trigger by checking for NULL on the id field
> > (like: IF ( NEW.ID IS NULL ) THEN NEW.ID = GEN_ID ....)

> My current trigger is as follows:
> CREATE TRIGGER ACCOUNTS_BI FOR ACCOUNTS
> ACTIVE BEFORE INSERT POSITION 0
> AS
> BEGIN
> IF (NEW.ID IS NULL) THEN
> NEW.ID = GEN_ID(GEN_ACCOUNTS_ID, 1);
> END ^
> commit ^
> SET TERM ; ^
>
> but how does this help?!

this trigger is okay, so it will only generate a new ID if you haven't
supllied one.

Luc.