Subject Re: [ib-support] Setting a generator to the maximum value of the primary key
Author Lucas Franzen
Stevio schrieb:
>
> Thanks Luc.
>
> The following code, modified from what you gave me, does what I am looking
> for fine:
> SELECT GEN_ID(GEN_NEW_AUTHOR_ID, (SELECT MAX (AUTHOR_ID) FROM AUTHOR)) FROM
> RDB$DATABASE
>
> I don't need to add 1 to it, as I understand, because the first time the
> code is used it will give me the next value, not the current value, for the
> new record being added. If this is wrong please tell me.

You're right.
Sometimes I'm extremely over-cautious :-)

>
> Also, I tried this code without the SELECT .... FROM RDB$DATABASE parts code
> and it didn't work. Why do I need this "select from" round the generator
> code?

You can select "against" any table if you like to.

Just try sth. like: SELECT 'HELLO WORLD' from MYTABLE (where 'HELLO
WORLD' is not a field name of Mytable, but a constant string).
The result will be n-times 'HELLO WORLD' where n is the number of
records in MYTABLE.

Since RDB$DATABASE has also exactly one record, and SELECT GEN_ID ... is
nothing else then asking for a generator value, you can use this trick
to achieve what you want, this statement will be "executed" exactly one
time.

Regards
Luc.