Subject | Re: [Firebird-Java] return ID after insert?? |
---|---|
Author | Rick Fincher |
Post date | 2004-01-12T17:50:10Z |
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
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