Subject Re: [ib-support] getting the generated ID
Author Lucas Franzen
zifnabbe schrieb:
>
> Hi,
>
> How can I get the generated key after inserting a record?
> Ie. I need to know which value firebird gave to the inserted record,
> this value will be used in an application. This to avoid refreshing
> the data everytime when a new record is generated.
>
> Is this possible?

Yes, but in this case you have to generate the value on the client side.

It depends how youre using Interbase. If you use IBObjects it's quite
easy, by using the GeneratorLinks or the Gen_ID function the
IB_Connection.

If you're not using IBObjetcs there are several ways to do it, for
example:

1. You can get a generator value from a Query by:
SELECT GEN_ID ( <GENERATORNAME>, 1 ) FROM RDB$DATABASE

2. You can write a stored procedure that can handle all generators you
have like:
create procedure sp_genid ( GENNAME VARCHAR(31) )
RETURNS ( NEW_ID INTEGER )
as
begin
if ( GENNAME = 'GENERATOR_1' ) THEN NEW_ID = GEN_ID (
G_GENERATOR_1, 1 );
else if ( GENNAME = 'GENERATOR_2' ) THEN NEW_ID = GEN_ID (
G_GENERATOR_2, 1 );
else if ( GENNAME = 'GENERATOR_3' ) THEN NEW_ID = GEN_ID (
G_GENERATOR_3, 1 );
... and so on ....
end

and surely there are several other ways.

But if you're assigning the generator value on server side (ie within a
BEFORE INSERT trigger) there's no way to get the value back to the
client (unless you can identify your inserted record uniquely by another
value and select it this way)

HTH

Luc.