Creating Generators (Embedded SQL Guide)

From InterBase

Go Up to Creating Metadata


A generator is a monotonically increasing or decreasing numeric value that is inserted in a field either directly by a SQL statement in an application or through a trigger. Generators are often used to produce unique values to insert into a column used as a primary key.

To create a generator to use in an application, use the following CREATE ­GENERATOR syntax:

EXEC SQL
CREATE GENERATOR name;

The following statement creates a generator, EMP_NO_GEN, to specify a unique employee number:

EXEC SQL
CREATE GENERATOR EMP_NO_GEN;
EXEC SQL
COMMIT;

Once a generator is created, the starting value for a generated number can be specified with SET GENERATOR. To insert a generated number in a field, use the InterBase library GEN_ID() function in an assignment statement. For more information about GEN_ID(), CREATE GENERATOR, and SET GENERATOR, see the Data Definition Guide.

Advance To: