Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Data from a SQL Query. 3

Status
Not open for further replies.

NewToProgramming

Technical User
Jun 24, 2003
31
US
I have created a SQL statement to sum up one of my fields. My question is how do you pull that data out of the SQL into some kind of integer variable.
Ex:
Here is the SQL Statement:
Query.SQL.Add ('Select SUM (Field) from DatabaseName')

Now take that number and place it into:
var
I : Integer(or real,double, whatever you think is best)
begin
I := that SQL SUM answer
 
NewToProgramming,

Something along these lines should work:

Code:
i := Query.fields[0].AsInteger;

Make sure that the query actually ran, e.g:

Code:
with query do
   if active then
      i := fields[0].AsInteger
   else
      raise Exception.create( 'Bad query; no donut.' );

Hope this helps...

-- Lance
 
Code:
var
    i :integer;
begin
Query.Database := databasename;  //set query's database
Query.Close;                     //make sure its closed
Query.SQL.Clear;                 //clear any previous strings
//add new query
Query.SQL.Add('Select SUM (Field) as total from TableName');
Query.Open;                     //open query

i := FieldByName('total').AsInteger;
Query.Close;
end;

hope it helps
imho fields[0].AsInteger  is not as maintainable, what if you add a field to your query?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top