Subject Re: [firebird-support] Get the value of generated primary key after an insert
Author Helen Borrie
At 07:57 AM 21/05/2006, you 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 ?

There is *no* way in Fb 1.5.x to know which value was assigned to
which inserted record. So what you do is get the generator's next
value *before* you post your INSERT statement and include it in the
fields and values of the statement.

This very handy mechanism is made possible by the fact that a
generator never generates the same number more than once (unless you
do something stupid by tinkering with the generator).

You *DO* have to make sure that you write your Before Insert trigger
so that it is aware of the possibility that this mechanism will be
used. See http://firebird.sourceforge.net/index.php?op=faq#q0011.dat

Firebird 2 has an enhanced syntax available to return the value[s] of
one or more columns from an INSERT statement. See the release notes, pp 35-6.

./heLen