Returning new ID in TPGSQL class

Discussion of open issues, suggestions and bugs regarding PgDAC (PostgreSQL Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
durumdara
Posts: 6
Joined: Fri 29 Apr 2011 11:16

Returning new ID in TPGSQL class

Post by durumdara » Tue 16 Oct 2018 08:01

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

ViktorV
Devart Team
Posts: 3168
Joined: Wed 30 Jul 2014 07:16

Re: Returning new ID in TPGSQL class

Post by ViktorV » Fri 19 Oct 2018 11:40

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]));

Post Reply