Subject Re: [firebird-support] Re: Auto increment
Author Martijn Tonies
> > > > 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.
>
> Hmm, looks like a newbie to me. If so, I think a warning about the
potential
> problems with concurrency and making these fields a composite key should
be
> mentioned (although, Martijn, I do know that you consider Dutch people
like
> yourself and Alexander (presumably) somewhat more clever than the rest of
us -
> and as such cannot imagine any of you stumbling at such an easy hurdle
;o).

Alright alright...

<standard warning>
This isn't multi user safe unless you make Field1 + Field2 a Unique or PK
Constraint.
</standard warning>

:-)

Happy Set?

With regards,

Martijn Tonies
Database Workbench - developer tool for InterBase, Firebird, MySQL & MS SQL
Server.
Upscene Productions
http://www.upscene.com