Subject Re: [firebird-support] Get the value of generated primary key after an insert
Author Radu Sky
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 ?
>> 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).
>> 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 ?
> Sorry but i'm really a novice.
> Thanks
> Adriano

Sorry, I didn't realize what you need.
I don't know if you actually need the PK value for some other reasons (I
use this for bulk master-detail inserts)
You don't need the get the generator value for insert if you just use it
as PK and you don't need it elsewhere. You can do something like
Whenever you get the GEN_ID(GEN_PRATICHE_IDPRATICA, 1) (either as
variable for later inserts or just as the INSERT above) it is guaranteed
to be unique, the other users will get the next values, no matter if
your work isn't done yet.