Access recent value of an AUTOINC field
Access recent value of an AUTOINC field
Hi,
just wondering how to access the recent assigned value of an autoinc field after inserting a new record into a table which primary key uses an generator to increment the primary key id.
cu,
Michael
just wondering how to access the recent assigned value of an autoinc field after inserting a new record into a table which primary key uses an generator to increment the primary key id.
cu,
Michael
Plash,
I've now tried this in Firebird 2.1 with a query but don't get this running.
I've got a table 'activities' with a generator:
And there is an OnBefore trigger on insert:
I've set qyActivity.DMLRefresh := True and qyActivity.RefreshOptions := [].
Now I'm stuck. I don't know how I could retrieve the number in ACTIVNO after inserting a new record into table 'activities'. The new record is inserted into the table of course and gets a new ID assigned. I don't know which event(?) to use to get the ID of this newly inserted row.
I'm using a client dataset, btw. This means the TIBCQuery is linked to a TDataSetProvider, the provider linke to a TClientDataset.
I've now tried this in Firebird 2.1 with a query but don't get this running.
I've got a table 'activities' with a generator:
Code: Select all
CREATE GENERATOR ACTIVITIES_ACTIVNO_GEN;
SET GENERATOR ACTIVITIES_ACTIVNO_GEN TO 3404;
Code: Select all
BEGIN
IF ((NEW.ACTIVNO IS NULL) OR (NEW.ACTIVNO <= 0)) THEN
NEW.ACTIVNO = GEN_ID(ACTIVITIES_ACTIVNO_GEN, 1);
END
Now I'm stuck. I don't know how I could retrieve the number in ACTIVNO after inserting a new record into table 'activities'. The new record is inserted into the table of course and gets a new ID assigned. I don't know which event(?) to use to get the ID of this newly inserted row.
I'm using a client dataset, btw. This means the TIBCQuery is linked to a TDataSetProvider, the provider linke to a TClientDataset.
The current IBDAC version does not generate INSERT SQL statement automatically if you apply updates using TClientDataSet and TDataSetProvider. So you should manually set the SQLInsert property of TIBCQuery to a statement with RETURNING. For example:
You can use IBCQuery.FieldByName('ACTIVENO').AsInteger to get a new value.
Code: Select all
INSERT INTO TABLE1(C2, C3) VALUES (:C2, :C3)
RETURNING ACTIVENO