How to use RETURNING in TUniquery

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
gn7soft
Posts: 8
Joined: Tue 10 Mar 2015 06:23

How to use RETURNING in TUniquery

Post by gn7soft » Tue 10 Mar 2015 06:44

Hello,

I am using TUniQuery (UniDac Professional, no source code) for Firebird.
In Firebird database, there are a serial type field (for autoincrement)
and Trigger (this trigger generates serial number to upper autoincrement field, when an insert is performed.)

I tried to use the "insert into ... returning" syntax in a parametrized TUniQuery in order to get the generated key value, but I cannot get proper result. Returned value(ID) has null.

Please help me! (Delphi7, Unidac6.0.2, Firebird 2.1)

Code: Select all

var ID:integer;

Try
   If DM.UniConn.InTransaction=False Then DM.UniConn.StartTransaction;

   with Uniquery do
   begin
    close;
    sql.clear;
    sql.add('Insert Into CONSULTANT (Name,JIKWI,PART) values(:Name,:JIKWI,:PART) Returning ID');
    ParambyName('Name').DataType:=ftString;
    ParambyName('Name').ParamType:=ptInput;
    ParambyName('JIKWI').DataType:=ftString;
    ParambyName('JIKWI').ParamType:=ptInput;
    ParambyName('PART').DataType:=ftString;;
    ParambyName('PART').ParamType:=ptInput;
 
    with TParam(Params.Add) do begin
      Name:='ID';
      DataType:=ftInteger;
      ParamType:=ptOutput;
    end; 

    ParambyName('Name').AsString:=trim(Ed_Name.Text);
    ParambyName('JIKWI').AsString:=trim(Ed_JikWi.text);
    ParambyName('PART').AsString:=trim(Ed_Part.text);

    Execsql;
   
    ID := ParamByName('ID').Value; 
   end;
   DM.UniConn.CommitRetaining;
Except
   DM.UniConn.RollbackRetaining
End;   


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

Re: How to use RETURNING in TUniquery

Post by ViktorV » Tue 10 Mar 2015 08:36

For the fields, that are specified after RETURNING, there are automatically generated output parameters with the RET_ prefix. This is described in the documentation: http://www.devart.com/ibdac/docs/index. ... ataset.htm
To solve the problem, in you sample:
1) don't create an out-parameter "ID";
2) use

Code: Select all

ID := ParamByName('RET_ID').Value;
instead of

Code: Select all

ID := ParamByName('ID').Value;
.

gn7soft
Posts: 8
Joined: Tue 10 Mar 2015 06:23

Re: How to use RETURNING in TUniquery

Post by gn7soft » Wed 11 Mar 2015 01:54

This works perfectly. Thanks.

Regards
Peace

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

Re: How to use RETURNING in TUniquery

Post by ViktorV » Wed 11 Mar 2015 12:00

Feel free to contact us if you have any further questions about UniDAC.

Post Reply