I Would like to know how Can I put the SQL Query result into a variable.
I'm aware of this
integerVariable := UniQuery1.RecordCount;
but this?
integerVariable := SELECT COUNT(*) FROM Orders WHERE Amount='1000'
what you need to do is first "execute" the sql, then check for result, if result is present, then store it in a variable, here's what I mean:
procedure ...;
var
LCount: Integer;
begin
LCount := 0;
//
// note that I am doubling the single quote to escape it
//
// set the query
UniQuery1.SQL.Text := 'SELECT COUNT(*) FROM Orders WHERE Amount=''1000'';';
//
// "execute" it
//
UniQuery1.Open;
//
// SELECT COUNT(*) will return 1 record with 1 field
// most likely the field name is 'count' <= lower case
// but we are sure that there should be only 1 field so we
// access it by Fields[Index].As[TYPE]
//
LCount := UniQuery1.Fields[0].AsInteger;
ShowMessageFmt('Total count of orders with Amount = 1000: %d', [LCount]);
end;
EDIT: thank you for point out that "COUNT" will always have a return.
COUNT() always returns a value, never a NULL. So, the not IsEmpty test can be omitted in this case.UniQuery1.FindFirst is also not needed.TUniQuery but isn't the dataset already on the first row after performing TUniQuery.Open ? Wouldn't be enough something like UniQuery1.Open; LCount := UniQuery1.Fields[0].AsInteger; ?count. to specify a name use select count(*) as as RecordCount and use UniQuery1.FiledByName('RecordCount').AsInteger. also better to use Parameters in your SQL.Text instead of doubling the single quote.
UniQuery1.Fields[0].AsIntegerif you are on the first row of your dataset, but we don't even know what type is theUniQuery1.this way. It will help the future visitors to recognize the right solution. Thanks and enjoy StackOverflow!