Subject Re: [firebird-support] Re: Generators for auto-increment - with override
Author Pierre Y.
>If I want to implement "generator protection" - where should
>I place it?

Here's how I implemented that :

CREATE TABLE TEST (TEST_ID INTEGER, CONSTRAINT TEST_PK PRIMARY KEY (TEST_ID));

CREATE TRIGGER TRG_TEST_GEN_ID FOR QB_TEST1
ACTIVE BEFORE INSERT POSITION 0
AS
declare variable GEN_VALUE Integer;
begin
if (new.TEST_ID is NULL) then
new.TEST_ID = gen_id(GEN_TEST_ID,1);
else
begin
GEN_VALUE = gen_id(GEN_TEST_ID,0);
if (new.TEST_ID > GEN_VALUE) then
GEN_VALUE = gen_id(GEN_TEST_ID,new.TEST_ID - GEN_VALUE);
end
end

I've tested in some cases, it seems to work well event if previous insert failed (I just loose a generator value and make a "hole" that's not so important)

Regards,

Pierre


[Non-text portions of this message have been removed]