Page 1 of 1

Returning new ID in TPGSQL class

Posted: Tue 16 Oct 2018 08:01
by durumdara
Hello!

I saw an example with PGQuery which can return the last inserted id.

But could I use same technic with TPGSQL class?

For example:
insert into ins_test(name) values (:name) returning id into :id

Could this class retreive the inserted record id somehow? Or is it available only in TPGQuery records?

Thanks
dd

Re: Returning new ID in TPGSQL class

Posted: Fri 19 Oct 2018 11:40
by ViktorV
To solve your task, you can use the following code:

Code: Select all

var
  Param: TDAParam;
...
  PgSQL.SQL.Text := 'insert into ins_test(name) values (:name) returning id';

  PgSQL.ParamByName('name').AsString := 'Name';

  Param := PgSQL.Params.Add as TDAParam;
  Param.Name := 'id';
  Param.ParamType := ptOutPut;
  Param.DataType := ftInteger;

  PgSQL.Execute;

  ShowMessage(Format('ID: %s', [PgSQL.ParamByName('id').AsString]));