Subject RE: [ib-support] Re: getting the generated ID
Author C Fraser
This is how we do it (still do it this way even though using IBO)...

Have a procedure that returns the Generator value:
Eg:-----------------------------
CREATE PROCEDURE NEW_CONTACT
RETURNS (
NEW_ID INTEGER)
AS
BEGIN
New_Id = Gen_Id(Gen_Contact_Id, 1);
END
-----------------------------

Then in the trigger we have a check to see if the id is already filled
in, if not we put fill it in by calling the procedure (we could also
have just used the generator here).
Eg:-------------------------------
CREATE TRIGGER CT_CONTACT_BI0 FOR CT_CONTACT
ACTIVE BEFORE INSERT POSITION 0
As
Begin
If (New.Contact_Id is Null) then Begin
Execute Procedure New_Contact Returning_Values New.Contact_Id;
End

New.UC_Name = Upper(New.Name);
New.Soundex_Code = F_Generatesndxindex(New.Name);
New.Time_Stamp = Current_Timestamp;
end
-----------------------------

In code (Delphi) we would get the id of the object by calling the
procedure, this ensures that we have used a unique key that cannot be
used again, we use this key when saving the record back to the
Database... The trigger sees that the ID is not nil so doesn't try and
get a new one... Doing it this way still allows us to enter data into
the database manually without having to worry about the id (if we leave
it out a unique one still gets inserted).

Regards
Colin

-----Original Message-----
From: zifnabbe [mailto:zifnabbe@...]
Sent: Friday, 14 December 2001 11:44 a.m.
To: ib-support@yahoogroups.com
Subject: [ib-support] Re: getting the generated ID


What is the function to retrieve the ID which is inserted by the
insert stored procedure? Or do you mean, creating a stored procedure
which calls the GEN_ID itself and thus not using the before insert
trigger?

Regards, Tom.

--- In ib-support@y..., Bill Morrison <bmorrison@n...> wrote:
> In IBO's case it pulls the ID from the server (a generator on the
server).
> Thus it is safe.
>
> If you're looking for another way, you could always use a stored
procedure
> to insert the record and return the ID.
>
>
> Bill
>
> -----Original Message-----
> From: zifnabbe [mailto:zifnabbe@u...]
> Sent: Thursday, December 13, 2001 12:36 PM
> To: ib-support@y...
> Subject: [ib-support] Re: getting the generated ID
>
>
> Isn't it dangerous to allow the client to generate an ID?
>
> Bummer, pitty it isn't possible. I know MySQL has a way of
determing
> it. Now I need to find another solution.
>
> Tom.
>
> --- In ib-support@y..., Lucas Franzen <luc@r...> wrote:
> >
> >
> > zifnabbe schrieb:
> > >
> > > Hi,
> > >
> > > How can I get the generated key after inserting a record? Ie. I
> > > need to know which value firebird gave to the inserted
> record,
> > > this value will be used in an application. This to avoid
> refreshing
> > > the data everytime when a new record is generated.
> > >
> > > Is this possible?
> >
> > Yes, but in this case you have to generate the value on the
client
> side.
> >
> > It depends how youre using Interbase. If you use IBObjects it's
> quite
> > easy, by using the GeneratorLinks or the Gen_ID function the
> > IB_Connection.
> >
> > If you're not using IBObjetcs there are several ways to do it, for
> > example:
> >
> > 1. You can get a generator value from a Query by:
> > SELECT GEN_ID ( <GENERATORNAME>, 1 ) FROM RDB$DATABASE
> >
> > 2. You can write a stored procedure that can handle all
generators
> you
> > have like:
> > create procedure sp_genid ( GENNAME VARCHAR(31) )
> > RETURNS ( NEW_ID INTEGER )
> > as
> > begin
> > if ( GENNAME = 'GENERATOR_1' ) THEN NEW_ID = GEN_ID (
> > G_GENERATOR_1, 1 );
> > else if ( GENNAME = 'GENERATOR_2' ) THEN NEW_ID = GEN_ID (
> > G_GENERATOR_2, 1 );
> > else if ( GENNAME = 'GENERATOR_3' ) THEN NEW_ID = GEN_ID (
> > G_GENERATOR_3, 1 );
> > ... and so on ....
> > end
> >
> > and surely there are several other ways.
> >
> > But if you're assigning the generator value on server side (ie
> within a
> > BEFORE INSERT trigger) there's no way to get the value back to the
> > client (unless you can identify your inserted record uniquely by
> another
> > value and select it this way)
> >
> > HTH
> >
> > Luc.
>
>
>
> To unsubscribe from this group, send an email to:
> ib-support-unsubscribe@e...
>
>
>
> Your use of Yahoo! Groups is subject to
http://docs.yahoo.com/info/terms/



To unsubscribe from this group, send an email to:
ib-support-unsubscribe@egroups.com



Your use of Yahoo! Groups is subject to
http://docs.yahoo.com/info/terms/



######################################################################
Attention:
The information in this email and in any attachments is confidential.
If you are not the intended recipient then please do not distribute,
copy or use this information. Please notify us immediately by return
email and then delete the message from your computer.
Any views or opinions presented are solely those of the author.
######################################################################