Subject Re: [firebird-support] Getting value of ID
Author Martijn Tonies
Hello Alex,

> Is this a safe implementation of getting primary key value inside a SP?

No.

> Or is there a chance for getting wrong values?

Yes.

>Is it possible for other
> users to insert additional records between the insert and the call to
> GEN_ID (inside the same proc.)?

Exactly.

>Of course there is a BEFORE INSERT
> trigger with appropriate code to generating the primary key value from a
> generator, if it is not passed in the insert values.
>
> CREATE TRIGGER TABLE_NAME_BI FOR TABLE_NAME
> ACTIVE BEFORE INSERT POSITION 0
> AS
> BEGIN
> IF (NEW.ID IS NULL) THEN
> NEW.ID = GEN_ID(TABLE_NAME_ID,1);
> END
>
> |CREATE PROCEDURE TABLE_NAME_I(
> INPUT_PARAMS...)
> RETURNS (
> ID INTEGER)
> AS
> BEGIN

ID = gen_id(gen_table_name_id, 1);

> INSERT INTO TABLE_NAME (id, FIELD_LIST)
> VALUES (:id, VALUE_LIST);
>
> END

With regards,

Martijn Tonies
Database Workbench - developer tool for InterBase, Firebird, MySQL & MS SQL
Server
Upscene Productions
http://www.upscene.com