Subject | Re: A bit strange GEN_ID result |
---|---|
Author | ionokot_k |
Post date | 2004-02-21T12:51:05Z |
--- In firebird-support@yahoogroups.com, Paul Vinkenoog <paul@v...>
wrote:
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
with ADO in an application.
IK
wrote:
> Hi ionokot_k,INTO :var;
>
> > 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"
> > insert into "Table1" ("ID") VALUES (:var);Yesssssss it's true!!
> > 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:
>
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
>There was only a test procedure. I would like to use a similar SQL
> Anyway, this procedure doesn't make much sense.
>
with ADO in an application.
> > With anI know... I'm complicated ;)
> > "INSERT INTO "Table1" ("ID") VALUES (GEN_ID("Table1_ID_GEN",1))
> > works fine...
>
> Yes, that's how to do it!
> Greetings,Thank you Mr. Vinkenoog!!
> Paul Vinkenoog
IK