Subject Re: Auto increment
Author a_broekhuis
--- In firebird-support@yahoogroups.com, "Martijn Tonies"
<m.tonies@u...> wrote:
>
> >
> > Yes you are right, I made a mistake, I meant I think that a trigger is
> > needed for this. The problem is I don't know how to write such a
trigger.
> >
> > Can this be done with a trigger?
>
> That depends. What do you want done when inserting a new row?
> Set the <field2> value based on <field1>?
>
> If so, you could try:
>
> BEFORE INSERT trigger:
>
> declare variable max_field2 integer;
> begin
> select max(field2) from mytable where field1 = new.field1
> into max_field2;
> if (max_field2 is null)
> then max_field2 = 0; /* zero rows found */
> new.field2 = max_field2 + 1;
> end;


Yes this looks like what I need. I will try it.

Thx!

Alexander