K
Seeing the code of the current implementation of TFDQuery, I see clearly that by using the method OpenOrExecute There is no way to collect the value of the affected rows. The method
Just try open the result of the command, as if it were a sentence select and ignores the exception that occurs if the sentence does not produce at least one set of data as a result.The underlying way to get the number of rows affected by a sentence insert/update/delete It's different.One way you could work on the-render of this limitation is to prepare the command and, depending on the type of sentence that FireDAC's own driver determines that it is to open or execute it, in which case the property RowsAffected is updated correctly.The basic idea is:Join the SQLPrepare the sentenceTake a look at the value of the property CommandKind property Command of TFDQuery, that will have one of the values skSelect, skInsert, skUpdateamong others.Depending on that value, you decide which method to execute (Open, ExecSQL, OpenOrExecute)If executed Open or ExecSQL, the property RowsAffected will show you correctly the number of affected rows.The code I've tried is:begin
FDQuery1.SQL.Text := 'tu sentencia aquí';
FDQuery1.Prepare;
if Assigned(FDQuery1.Command) then
case FDQuery1.Command.CommandKind of
skNotResolved,
skUnknown: FDQuery1.OpenOrExecute;
skSelect,
skSelectForLock,
skSelectForUnLock: FDQuery1.Open;
skDelete,
skInsert,
skMerge,
skUpdate,
skCreate,
skAlter,
skDrop,
skStoredProc,
skStoredProcWithCrs,
skStoredProcNoCrs,
skExecute,
skStartTransaction,
skCommit,
skRollback,
skSet,
skSetSchema,
skOther: FDQuery1.ExecSQL;
end
else
FDQuery1.OpenOrExecute;
Label2.Caption := IntToStr(FDQuery1.RowsAffected);
end;
I've tested SQL Server and this has worked me reasonably well. I suggest that before you implement it you also try it with Oracle (since it depends on the specific driver of the database), and also that you make sure to perform the correct operation for each type of sentence (I have done a quick test, but I cannot guarantee the result in cases like skSelectForLock or equally exotic values.