What is the proper way to use the Firebird "insert ... returning"
with dbxpress?
I have tried the following code, but errors with "List index out of bounds", indicative
of its failure to return a dataset. I have tried changing q.ExecSql
to q.Open
but errors with "Result set not returned by query" or something like that. Appears to be
the same problem, dataset not able to return the desired resultset.
procedure TdmMain.mdp_a_sqlBeforeUpdateRecord(Sender: TObject; SourceDS:
TDataSet; DeltaDS: TCustomClientDataSet; UpdateKind: TUpdateKind; var
Applied: Boolean);
const
SQL = 'insert into A_SQL( ID, FORM_NAME, TABLE_NAME, STATEMT )'#13#10+
' values(-1,''%s'',''%s'',''%s'') '#13#10+
' returning id';
var
s : string;
rs: TCustomSQLDataSet;
id: Integer;
params: TParams;
q: TSQLQuery;
begin
if UpdateKind = ukInsert then
begin
s := Format( SQL, [DeltaDS.FieldByName('FORM_NAME').AsString,
DeltaDS.FieldByName('TABLE_NAME').AsString,
DeltaDS.FieldByName('STATEMT').AsString]);
params := TParams.Create;
q := TSQLQuery.Create(Self);
try
params.CreateParam(ftInteger,'ID',ptOutput);
q.SQL.Add(s);
q.SQLConnection := AppConnection;
q.Params.Assign(params);
q.ExecSQL;
id := q.Fields[0].AsInteger;
DeltaDS.FieldByName('ID').AsInteger := id;
Applied := True;
finally
params.Free;
q.Free;
end;
end;
end;