Board index » delphi » How to save a SQL-Sum-Calculation to a variable

How to save a SQL-Sum-Calculation to a variable

Hi everybody,

I need help. I want to save the result of a sql-sum-calculation in a
variable. I know that i can do the way to use variables in a
sql-statement, but i don`t know how i can go the other way.

with best regards
Christian Huber

 

Re:How to save a SQL-Sum-Calculation to a variable


If I understand the question

{Private Declarations}
MyVariable : Integer;
....
Query.Open;
MyVariable := query.Field[0].Value;

Robert

Quote
Christian Huber <christian1.hu...@stud.fh-regensburg.de> wrote in message

news:374D3FCF.BE455A78@stud.fh-regensburg.de...
Quote
> Hi everybody,

> I need help. I want to save the result of a sql-sum-calculation in a
> variable. I know that i can do the way to use variables in a
> sql-statement, but i don`t know how i can go the other way.

> with best regards
> Christian Huber

Re:How to save a SQL-Sum-Calculation to a variable


On Thu, 27 May 1999 14:51:28 +0200, Christian Huber

Quote
<christian1.hu...@stud.fh-regensburg.de> wrote:
>I need help. I want to save the result of a sql-sum-calculation in a
>variable. I know that i can do the way to use variables in a
>sql-statement, but i don`t know how i can go the other way.

To retrieve the value of a column in a query result set, use properties or
methods of the TField class (or a descendent class). Obtain a TField
reference using the Fields property or the FieldByName method of the
TQuery.

The Fields property allows you to reference a column by its relative
position in the result set, with the first column being zero. For example,
assuming the SQL statement...

  SELECT SUM(AmountPaid)
  FROM Orders
  WHERE (CustNo = 1221)

...the summarized column is the first column and so an index of zero would
be used with the Fields property. Use a property of TField such as AsFloat
to retrieve the column's values through the TField.

  var
    YourVar: Double;
  begin
    Query1.Open;
    YourVar := Query1.Fields[0].AsFloat;
    ...
    Query1.Close;
  end;

The FieldByName method allows you to reference a column by its name. For
aggregated columns in query result sets, provide a column correlation name
for this name reference.

  SELECT SUM(AmountPaid) AS TheSum
  FROM Orders
  WHERE (CustNo = 1221)

Then, use that column correlation name with the FieldByName method.

  var
    YourVar: Double;
  begin
    Query1.Open;
    YourVar := Query1.FieldByName('TheSum').AsFloat;
    ...
    Query1.Close;
  end;

//////////////////////////////////////////////////////////////////////////
Steve Koterski                   "There are two kinds of pedestrians...the
Technical Publications           quick and the dead."
INPRISE Corporation                            -- Lord Thomas Robert Dewar
http://www.borland.com/delphi                                  (1864-1930)

Other Threads