SQL クエリの結果を変数に入れる方法を知りたいです。
私はこれを知っています
integerVariable := UniQuery1.RecordCount;
でもこれは?
integerVariable := SELECT COUNT(*) FROM Orders WHERE Amount='1000'
SQL クエリの結果を変数に入れる方法を知りたいです。
私はこれを知っています
integerVariable := UniQuery1.RecordCount;
でもこれは?
integerVariable := SELECT COUNT(*) FROM Orders WHERE Amount='1000'
あなたがする必要があるのは、最初にSQLを「実行」してから結果をチェックし、結果が存在する場合はそれを変数に格納することです。
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;
編集:「COUNT」には常にリターンがあることを指摘していただきありがとうございます。