I came across a curious behaviour today.
I use auto inc fields (with generator) in a firebird database and use the sql
Code: Select all
INSERT INTO (MyValue) VALUES (:Valor) RETURNING DocId
But I get the "RET_DocId" not found error when I try to read the Out-Param. When I switched to my dynamically loaded SQL. it works as expected.
BTW: I use a standard function I wrote to run these inserts with returning id as optional parameter and it works fine with all other forms (which load dynamically SQL at run time. :
Code: Select all
function TDMWXSI.DoTrySave_Document(DS: TDataSet; Q: TUniQuery; const Return_FieldName: String = ''): Int64;
var bAgain: Boolean;
AField : TField;
Numero : Integer;
begin
Result := -1;
AField := DMWXSI.FindField(DS, 8004); // looking for field 'Numero'
If Assigned(AField) Then
Numero := AField.AsInteger // If we have a dataset to get the initial number from...
Else
Numero := Q.ParamByName('Numero').AsInteger; // if no we get the initial number from the param Numero
bAgain := True;
While bAgain Do begin
Try
Q.ParamByName('Numero').AsInteger := Numero;
Q.ExecSQL;
If Assigned(AField) Then
If AField.AsInteger <> Numero Then begin
DS.Edit;
AField.AsInteger := Numero;
DS.Post;
end;
bAgain := False;
If Return_FieldName <> '' Then
Result := Q.ParamByName('RET_' + Return_FieldName).AsLargeInt;
Except on E: Exception Do
If Pos('PRIMARY or UNIQUE KEY', E.Message) <> 0 Then
Inc(Numero)
Else
Raise Exception.Create(E.Message);
end;
end; // -- While bAgain Do
end;
Helge