Again about Get Identity

Discussion of open issues, suggestions and bugs regarding SDAC (SQL Server Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
Posts: 20
Joined: Wed 24 Aug 2005 09:28
Location: Africa

Again about Get Identity

Post by alec » Wed 04 Oct 2006 07:01

Good day!
Now we use such code for get identity:

Code: Select all

procedure TForm1.MSQuery1BeforeUpdateExecute(Sender: TCustomMSDataSet;
  StatementTypes: TStatementTypes; Params: TMSParams);
  if stInsert in StatementTypes then
    Params.ParamByName('id').ParamType := ptInputOutput;
I think it's silly work to write such handler for each component. I can assign one handler for all TMSQuery of course, but I have different identity field names such as "id_user","id_equipment" etc.
Is any way for automate task of get identity?
Thank you very much.

Posts: 512
Joined: Thu 26 Jan 2006 10:06

Post by Jackson » Wed 04 Oct 2006 09:28

You can clear SQLInsert property and SDAC will automatically generate INSERT statement and will take care of returning identity field value.
If it is not applicable for your task you can use construction like following:

Code: Select all

procedure TForm1.MSQuery2BeforeUpdateExecute(Sender: TCustomMSDataSet;
  StatementTypes: TStatementTypes; Params: TMSParams);
  i: integer;
  IdentityFieldName: string;
  for i := 0 to Sender.Fields.Count - 1 do
    if Sender.Fields[i].AutoGenerateValue = arAutoInc then begin
      IdentityFieldName := Sender.Fields[i].FieldName;

  if stInsert in StatementTypes then begin
    if IdentityFieldName = '' then
      raise Exception.Create('Identity field not found.');
    Params.ParamByName(IdentityFieldName).ParamType := ptInputOutput;
But parameters names in your INSERT statement must have same names as corresponding fields.

Post Reply