Retrieve Auto Increment Id DataSnap Delphi XE3

2

I use Delphi XE3 / DataSnap with Firedac

When I write the parent table on the server I want to retrieve the value that was added via auto increment in the parent so that I can enter this new value in the foreign key in the child table.

I do not know where to pick it and how to pick it up if it can be what event.

...AfterUpdateRecord(Sender: TObject;
  SourceDS: TDataSet; DeltaDS: TCustomClientDataSet; UpdateKind: TUpdateKind);

or

...BeforeUpdateRecord(Sender: TObject;
  SourceDS: TDataSet; DeltaDS: TCustomClientDataSet; UpdateKind: TUpdateKind;
  var Applied: Boolean);
    
asked by anonymous 10.06.2014 / 17:39

2 answers

1

On the server (TADQuery and TDataSetProvider), in the 'AfterPost' event of the TADQuery Master the Refresh command must be run, which will be updated with the self-increment ID that has just been executed "I created a FId: Integer variable to receive the returned value of the database for use in child dataset "

As the example below:

procedure TsrmNFSe.ADQueryAfterPost(DataSet: TDataSet);
begin
   ADQuery.Refresh;
   FId    := ADQuery.fieldbyname('id_campo_autoincremento').AsInteger;
end;
    
11.06.2014 / 21:08
0

The best way for you to do this is to use event [BeforeUpdateRecord][1] . In this event you can change the Delta that is being sent to the bank (even controlling the type of change that is happening. You can save the new ID inside a DM private variable (or the main object) and then use it in the items.

    
22.12.2017 / 04:23