Link to home
Start Free TrialLog in
Avatar of erhan_13
erhan_13

asked on

Assign a specific SQL result to Delphi variable

Hi everyone,
my question is very simple but couldnt find a good solution.
- I have a simple table EMP with fields  NAME, DEPARTMENT, SELARY  ...  I want to assign the result of a SQL query to a variable in Delphi (7) to use it in some calculations.

for example   select avg(selary) from EMP;  
    and assign that to Delphi variable declared as

var
avgselary : integer;
.....    

Thanks to all...
ASKER CERTIFIED SOLUTION
Avatar of SteveBay
SteveBay
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I like to wrapper this kind of thing in a method of my data module.  The database connection is already on the data module.  I use the GetQuery and ReleaseQuery functions to standardize how the query is created (setting it's connection, etc), and released.  IntQuery is designed to be passed a query that will return one row and one column.  In your case you might want to have your query cast the average to an int before using this.

Let me know if you need more.
function TdmMyDataModule.IntQuery(SQL: string): integer;
var
  Query : TADOQuery;
 
begin
  Query := GetQuery;
  try
    Query.SQL.Text := SQL;
    Query.Open;
    Result := Query.Fields[0].AsInteger;
  finally
    ReleaseQuery(Query);
  end;
end;

Open in new window