Subject RES: [firebird-support] Re: Autoincremented Fields in Delphi
Author Carlos Arguilar
Genilton,

First, declare the primary key like Not null Default 0.
In the trigger you do following:
...
IF (NEW.FIELD IS NULL OR NEW.FIELD = 0) THEN
NEW.FIELD = GEN_ID(GEN_NAME,1).
...
when insert a new record you mustn't to use this "field", this way
automatically it will store zero and the process will work properly.

good luck!

Regards,

Carlos Arguilar
-----Mensagem original-----
De: firebird-support@yahoogroups.com
[mailto:firebird-support@yahoogroups.com]Em nome de Genilton Barbosa
Enviada em: quarta-feira, 3 de agosto de 2005 15:39
Para: firebird-support@yahoogroups.com
Assunto: [firebird-support] Re: Autoincremented Fields in Delphi


I migrated a database from Paradox to Firebird unsing the migration
tool IBPump. IBPump create generators and triggers to simulate
autoinc fields.

The autoinc simulation is not working if the autoinc fields are used
in primary keys (NOT NULL constraint). It looks like the constraint
is checked before the execute of the trigger.

What can I do?

Genilton Barbosa
Brazil


--- In firebird-support@yahoogroups.com, Mitchell Peek <mitchp@h...>
wrote:
> gato2707 wrote:
>
> >Hi everybody:
> >
> >I'm trying to use an autoincrement field in a table, the field is
part
> >of the primary key. First I used a trigger calling a generator,
but
> >when insert a new record in my Delphi application i discover is
> >neccesary to write any value in the field BEFORE the post
operation,
> >otherwise an error occurs. Then I read best way to do that is
thru a
> >StoredProcedure, well, I cant't find where call that procedure.
Any
> >sugestion?
> >
> >
> >
> No, a trigger.
>
> Create Generator gen_MYNAME;
>
> >CREATE TABLE MYNAMES
> >(
> > MYCODE INTEGER,
> > MYNAME CHAR(30)
> >)
> >
> >CREATE trigger t_myname_bi for myname before insert as
> >AS
> >BEGIN
> >
> >
> if (new.mycode is null) then
>
> > NEW.myCODE = GEN_ID(gen_myname, 1);
> >END
> >
> >
> >
> >
> >
> >




++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Visit http://firebird.sourceforge.net and click the Resources item
on the main (top) menu. Try Knowledgebase and FAQ links !

Also search the knowledgebases at http://www.ibphoenix.com

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++




----------------------------------------------------------------------------
--
YAHOO! GROUPS LINKS

a.. Visit your group "firebird-support" on the web.

b.. To unsubscribe from this group, send an email to:
firebird-support-unsubscribe@yahoogroups.com

c.. Your use of Yahoo! Groups is subject to the Yahoo! Terms of
Service.


----------------------------------------------------------------------------
--



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