Using GetLastAutoGenValue
-
22-12-2019 - |
Question
I am using fireDAC TFDQuery to insert values in informix database, using below code snippet:
with QHeaderQuery do
begin
if not (Prepared) then
begin
{DatabaseName := Sessions.CurrentSession.Databases[0].DatabaseName;
SessionName := Sessions.CurrentSession.SessionName;}
Connection := FDManager.Connections[0];
SQL.Clear;
SQL.Add('insert into asptheade ( ');
SQL.Add('theade_trans, theade_store, theade_register, theade_location, ');
SQL.Add('theade_date, theade_cashier, theade_type, theade_name, ');
SQL.Add('theade_salesperson, theade_group, theade_group_disc, ');
SQL.Add('theade_comm_flag, theade_comm_price, theade_cust_code, ');
SQL.Add('theade_sub_total, ');
SQL.Add('theade_tax_total, theade_disc_total, theade_grand_total, ');
SQL.Add('theade_change_amt, theade_drawertime, theade_posted_date, theade_gratuity) ');
SQL.Add('values ( :d0, :d1, :s2, :s3, ');
SQL.Add(' current year to second, :s4, :s5, :s6, ');
SQL.Add(':s7, :s8, :f9, ');
SQL.Add(':s10, :s11, :s12, :f13, ');
SQL.Add(':f14, :f15, :f16, :f17, :f18, :f19, :f20 ) ');
Prepared := True;
end;
{SQL.Add( Format( 'values ( %d, %d, "%s", "%s", ', }
Params[0].AsInteger := FTrans;
Params[1].AsInteger := FStore;
Params[2].AsString := FRegister;
Params[3].AsString := FLocation;
{SQL.Add( Format( ' current, "%s", "%s", "%s", ', }
Params[4].AsString := FCashier;
Params[5].AsString := FTransType;
Params[6].AsString := FStatementText;
{SQL.Add( Format( '"%s", "%s", %f, ',}
Params[7].AsString := FSalesPerson;
Params[8].AsString := FDiscGroup;
Params[9].AsFloat := FDiscRate * 100;
{SQL.Add( Format( '"%s", "%s", "%s", %f, ',}
Params[10].AsString := FCommFlag;
Params[11].AsString := FCommPrice;
Params[12].AsString := FCustCode;
Params[13].AsFloat := FSubTotal;
{SQL.Add( Format( '%f, %f, %f, %f ) ',}
Params[14].AsFloat := FTaxTotal;
Params[15].AsFloat := FDiscTotal;
Params[16].AsFloat := FGrandTotal;
Params[17].AsFloat := FChangeAmt;
Params[18].AsDateTime := FDrawerTime;
Params[19].AsDateTime := FServiceDate;
Params[20].AsFloat := FGratuity;
Execute;
FValid := True;
end;
FTrans := FDManager.Connections[0].GetLastAutoGenValue('asptheade');
QHeaderQuery.Close;
I am getting Capability not supported. I even tried empty string instead of but again the same error. Can you please guide me with the proper usage of GetLastAutoGenValue? I am using dbExpress bridge driver.
Solution 3
I made it work out using following query in my stored proc:
SELECT DBINFO('sqlca.sqlerrd1') FROM SYSTABLES WHERE tabname = '<tablename>'
and return the same value in Stored Proc.
OTHER TIPS
GetLastAutoGenValue supports returning of:
- the last auto-incremental field value used at Insert / Post for all data sources;
- the last auto-incremental value used at ExecSQL / ExecProc for native FireDAC drivers;
- the last value of sequence / generator for native FireDAC drivers. The GetLastAutoGenValue argument is a generator / sequence name.
FireDAC XE5 does not have native driver for Informix. So, (2) and (3) are not supported for Informix. Your code is INSERT with Execute, so (1) is not supported too. Consequently you are getting "Capability not supported".
You can workaround by executing following query right after the insert query:
SELECT DBINFO('sqlca.sqlerrd1') FROM SYSTABLES WHERE tabname = 'systables'