Reputation: 101
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'
Upvotes: 4
Views: 14159
Reputation:
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.
Upvotes: 7