Subject Re: [firebird-support] Get the value of generated primary key after
Author Radu Sky
Radu Sky wrote:
> Adriano wrote:
>> Radu,
>> sorry i'm a bit confused.
>> I read on FB FAQ that:
>> Firebird doesn't currently return values from insert statements and, in
>> multi-user, it isn't safe to query the generator afterwards
>> (GEN_ID(Generator_name, 0) because you have no way to know whether the
>> current "latest" value was yours or someone else's. The trick is to get the
>> generator value into your application as a variable *before* you post your
>> insert. This way, you make it available not just for your insert but for any
>> dependent rows you need to create for it inside the same transaction.
>> So can you explain a bit more about that ?

GEN_ID(Generator_name, 0) retrieve the present value of the
generator.Somebody else already got it, maybe it wasn't you.

>>> When you do GEN_ID(GEN_PRATICHE_IDPRATICA, 1) you increment the generator.
>> why if i use a SELECT i increment the generator ?


>>> If you just want to retrieve the generator value use
>> But if i put BEFORE of the insert as said on faq and then use a INSERT query
>> the value isn't good (i use autoincrement trigger for master key).

Remove the autoincrement trigger if you need the generator value
elsewhere and put the variable value that you used to store it into the
INSERT statement.

>>> Generator are transaction independent so it is guaranteed that the
>>> returned value is unique (of course, unless you reset it, but that is
>>> off topic)
>> But the faq above do not said the opposite ?

No, when you will retrieve GEN_ID(Generator_name, 0) after insert, some
other users could also increment it.

>> Sorry but i'm really a novice.
>> Thanks
>> Adriano