Subject Re: A bit strange GEN_ID result
Author ionokot_k
--- In firebird-support@yahoogroups.com, Paul Vinkenoog <paul@v...>
wrote:
> Hi ionokot_k,
>
> > In order to get the last inserted id I made this stored as a test:
> >
> > CREATE PROCEDURE NEW_PROCEDURE
> > AS
> > declare variable var integer;
> > BEGIN
> > select max(GEN_ID("Table1_ID_GEN",1)) FROM "Table1"
INTO :var;
> > insert into "Table1" ("ID") VALUES (:var);
> > SUSPEND;
> > END
>
> That's not how to get the last inserted id. You either select the
> new generator value (if you need one), like this:
>
> select gen_id( "Table1_ID_GEN", 1 ) from rdb$database
>
> or you select the max ID value from the table (if you need it):
>
> select max( "ID" ) FROM "Table1"
>
> If you select a gen_id() from a table with more then one record, the
> generator will fire multiple times. In your case, all the newly
> generated values except the last one are lost (because you select
> the MAX). This explains the gaps:
>

Yesssssss it's true!!

This procedure works as I can expect:

CREATE PROCEDURE NEW_PROCEDURE
AS
declare variable var integer;
BEGIN
select gen_id("Table1_ID_GEN", 1 ) from rdb$database INTO :var;
insert into "Table1" ("ID") VALUES (:var);
SUSPEND;
END

>
> Anyway, this procedure doesn't make much sense.
>

There was only a test procedure. I would like to use a similar SQL
with ADO in an application.

> > With an
> > "INSERT INTO "Table1" ("ID") VALUES (GEN_ID("Table1_ID_GEN",1))
> > works fine...
>
> Yes, that's how to do it!

I know... I'm complicated ;)

> Greetings,
> Paul Vinkenoog

Thank you Mr. Vinkenoog!!

IK