Subject Re: [IBO] Generator Links at runtime
Author Helen Borrie (TeamIBO)
At 06:39 AM 06-07-02 +0000, you wrote:
>the TIB_Query that runs this is created on the fly just before this...
>
>
>SQL.TEXT := 'Select * from TABLE1';
>GeneratorLinks.Add('Table1_ID=gen_Table1_PK');
>Prepare;

So far, correct.

>TABLE1ID := GeneratorValue('gen_Table1_PK',1);

No. IBO fetches the generator value for you when it posts the delta record
to the database.

If you want to know it immediately, and pull it into a variable, there is a
different way to get it. Don't set GeneratorLinks at all, but instead,
before inserting, call the Gen_ID() method of either the IB_Connection or
the IB_Query:

var
Table1ID: integer;
begin
IB_Query1 := TIB_Query.Create(self);
with IB_Query1 do
try
IB_Connection := My_IB_Connection;
SQL.Add('Select * from TABLE1');
TABLE1ID := Gen_ID('gen_Table1_PK',1);

Prepare;
Open;
Insert;
Table1_PK := TABLE1ID;
....
Post;
...
finally
Close;
Free;
end;
end;

However, it's not really the right thing to do, to create an IB_Query on
the fly for the sole purpose of inserting a row. Since you don't need a
dataset, with all of its overhead, you should create an IB_DSQL and fire
your insert off that:

var
Table1ID: integer;
begin
IB_DSQL1 := TIB_DSQL.Create(self);
with IB_DSQL1 do
try
IB_Connection := My_IB_Connection;
TABLE1ID := Gen_ID('gen_Table1_PK',1);
SQL.Add('Insert into TABLE1 (');
SQL.Add('Table1_PK, ColA, ColB)');
SQL.Add('Values(TABLE1ID, :ColA, :ColB);
Prepare;
Params['ColA'].AsWhatever := SomeValue;
Params['ColB'].AsWhatever := SomeOtherValue;
Execute;
...
finally
Free;
end;
end;

If you have a loop of several rows to insert, only prepare the query once
and test at the start of the loop for
if not Prepared then Prepare;


regards,
Helen Borrie (TeamIBO Support)

** Please don't email your support questions privately **
Ask on the list and everyone benefits
Don't forget the IB Objects online FAQ - link from any page at
www.ibobjects.com