community.borland.com

Article #25167: How do I assign a generator to populate my primary key with triggers.

Problem:
How do I assign a generator to populate my primary key with triggers.
	

Solution:
Scenario:
	Assume that you have already created a generator in
	your database as:
		
	CREATE GENERATOR my_gen_id;
		
	NOTE:	The default value of a generator when created is
	(0) zero.  When you use the generator the first
	value assigned to your primary key will depend on
	the incriment value that you assign in your trigger.

	Example:
	
	Code:

	---------------snip-----------------------		
	SET TERM !!;
	
	CREATE TRIGGER get_unique_value FOR my_table
	BEFORE INSERT
	AS
	BEGIN
			
	NEW.field_x = GEN_ID(my_gen_id, 1);
				
	END !!
			
	SET TERM ;!!
	---------------snip-----------------------		
			
	NOTE:	Please notice that the increment value is
		1.  This tells the generator to
		increment by 1 every time it is called.
			
		Once the trigger is successfully created you can
		issue an INSERT statement against the related table,
		"my_table" and it will call GEN_ID, BEFORE the insert
		happens, thus generating a new primary key value for 
		you and then populate the table and related records.

Last Modified: 29-SEP-00