Fox query result to variable

cswebdev used Ask the Experts™
in MS sql we can store a query result to a variable like :
declare @dt as datetime
select top 1 @dt = age from table

how can i do similar thing in Fox Pro Query. I am using Visual Fox Pro 9
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Query result is possible to save into a Cursor, table, or array in VFP:

SELECT TOP 1 Something FROM Table ORDER BY Something INTO ARRAY laResult

If there are some records in the Table then laResult[1] will contain the lowest value of Something column. If there are no results then the array is not created/updated and you should test this possibility by _tally variable value check.

It is also possible to store M x N result matrix into an array.

Just to follow on with Pavel's advice above...

SELECT TOP 1 Something ;
   FROM MyTable ;
   WHERE <some criteria>
   ORDER BY <some field sequence>
   INTO CURSOR Results

* --- Select Resultant Cursor, Then put first record into Variable ---
SELECT Results
Variable = Results.Something   && Where 'Something' represents a field such as Age

Good Luck

And don't forget to close the cursor :-)
CaptainCyrilFounder, Software Engineer, Data Scientist

You can write it as a function.

You send a table name and a field name.

You can get the value and return it to a calling procedure like this:

Moreover, FoxPro has better ways for getting that:
CALCULATE MAX(field), MIN(field)  FOR condition TO nVariableMax, nVariableMin

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial