Subject Re: [firebird-support] Find Generator Value Used after Insert Trigger
Author Alexandre Benson Smith
garthtissington wrote:

>I gave a Generator and it is used in a trigger to automatically
>populate a primary key field. Let's say the primary key is KEY_ID
>
>When I execute a statement like
>
>INSERT (F1,F2,F3) values (1,2,3);
>
>KEY_ID is automatically generator correctly. Great.
>
>How can I find out which Generator ID value was used? I know if
>Could get the current generator value but it may not be accurate as
>there may have been an insert from another process between my insert
>and my retrieval of teh current GEN_ID.
>
>Incidentally, I'm Using Delphi and IBObjects.
>
>Thank you
>
>p.s. I know I could also get the gen ID and include it in the INSERT
>statement. This has a different problem in that I chew through a
>lot of ID numbers when proposed inserts are cancelled or aborted
>
>
IBO has a property to link generator, automatically, I don't use IBO so,
i can't help you on that.

The common behaviour is to get the generator before you do a insert
select gen_id(my_generator, 1) from rdb$database

But you said you will have missing values, I don't know your problem
with it, I just leave with the gaps... Take a look on an article on IBO
site, something like auditable series

Another way:
Select MyId from MyTable where Unique_Column = 'Your value';

Do it in a SP and return the generator value inserted as a parameter,
but here you will have the same problem, once you get a generator value,
and you insert fails for some reason you will have a gap.

I heard that on future FB versions will be an extension to insert
statement something like this:
insert into MyTable (ColumnA, ColumnB, ColumnC) values (null, 1, 2)
returning ColumnA

that will solve your problem...


see you !

--

Alexandre Benson Smith
Development
THOR Software e Comercial Ltda.
Santo Andre - Sao Paulo - Brazil
www.thorsoftware.com.br