Subject Re: [Firebird-Java] return ID after insert??
Author Alexandru Balut
That is, using

SELECT gen_id(my_generator, 100) FROM RDB$DATABASE

and then use the ids from (retrieved_value - 99) to (retrieved_value) ..

:)



----- Original Message -----
From: "Rick Fincher" <rnf@...>
To: <Firebird-Java@yahoogroups.com>
Sent: Monday, January 12, 2004 22:08
Subject: Re: [Firebird-Java] return ID after insert??


> One other helpful idea from Roman: If you have a lot of inserts to do,
> create a pool of ID's (primary keys) to use, then use them one by one as
> needed.
>
> Rick
>
> ----- Original Message -----
>
> > Hi Richard,
> >
> > I had this same problem, the answer is below. I'll add this to the FAQ
> > since it keeps coming up.
> >
> > You do have to do a two step process with a generator to get the Primary
> Key
> > (Pk below).
> >
> > To get a new primary key to insert with:
> >
> > SELECT gen_id(my_generator, 1) FROM RDB$DATABASE
> >
> > This fires your generator and returns the new key. The following sets
up
> > the trigger so that it will auto create the primary key if you don't
pass
> it
> > one, but will use one that you give it (generated above). So this works
> > both ways.
> >
> > Rick
> >
> > >
> > > The only way to get the value of a generated key into your app in a
way
> > > that will allow you to subsequently locate the row you just inserted
is
> to
> > > generate the number FIRST and pass it into your insertsql. If you
have
> an
> > > "autoinc trigger" on this PK, then you must modify the trigger so that
> it
> > > does a null test before firing, viz.
> > >
> > > create trigger...
> > > active before insert
> > > as
> > > begin
> > > if (new.YourPk is null) then
> > > new.YourPk = gen_id(YourGenerator, 1);
> > > end
> > >
> > > Trigger generation is outside of transaction control (the only thing
> that
> > > is!) so it is guaranteed to be 100% atomic.
> > >
> > > Helen
> >
> > ----- Original Message -----
> > From: "Richard Drent" <richard@...>
> > To: <Firebird-Java@yahoogroups.com>
> > Sent: Sunday, January 11, 2004 7:33 AM
> > Subject: [Firebird-Java] return ID after insert??
> >
> >
> > >
> > > Hi guy's and girl's,...
> > >
> > > Is there a way to get back the id of a record that just has been
> > > inserted,... with java..??
> > >
> > > gteerz
> > >
> > >
> > >
> > > Richard Drent
>
>
>
>
>
> Yahoo! Groups Links
>
> To visit your group on the web, go to:
> http://groups.yahoo.com/group/Firebird-Java/
>
> To unsubscribe from this group, send an email to:
> Firebird-Java-unsubscribe@yahoogroups.com
>
> Your use of Yahoo! Groups is subject to:
> http://docs.yahoo.com/info/terms/
>