Subject | Re: [firebird-support] Re: Generators for auto-increment - with override |
---|---|
Author | Pierre Y. |
Post date | 2005-01-04T19:51:24Z |
>If I want to implement "generator protection" - where shouldHere's how I implemented that :
>I place it?
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]