COUNT Command

Counts table records.

COUNT   [Scope] [FOR lExpression1] [WHILE lExpression2]   [TO VarName]
   [NOOPTIMIZE]

Parameters

  • Scope
    Specifies a range of records to be included in the count. The scope clauses are: ALL, NEXT nRecords, RECORD nRecordNumber, and REST. For more information on scope clauses, see the Scope Clauses topic. Commands that include Scope operate only on the table in the active work area.

    The default scope for COUNT is ALL records.

  • FOR lExpression1
    Specifies that only the records that satisfy the logical condition lExpression1 are counted. Including FOR lets you conditionally count records, filtering out undesired records.

    Rushmore will optimize a COUNT FOR query if lExpression1 is an optimizable expression. For best performance, use an optimizable expression in the FOR clause.

    For more information on optimizable expressions, see SET OPTIMIZE and Using Rushmore to Speed Data Access.

  • WHILE lExpression2
    Specifies a condition whereby records are counted for as long as the logical expression lExpression2 evaluates to True (.T.).

  • TO VarName
    Specifies the variable or array to which the record count is stored. If the variable you specify doesn't exist, Visual FoxPro creates it.

  • NOOPTIMIZE
    Disables Rushmore optimization of COUNT. For more information see SET OPTIMIZE and Using Rushmore to Speed Data Access.

Remarks

COUNT counts the records within a scope of records for which the FOR or WHILE conditions are true. If SET TALK is ON, the record count is displayed.

Records marked for deletion are included in the count if SET DELETE is OFF.

For a discussion of how null values affect COUNT, see the Overview of the Language topic.

Example

The following example counts and displays the number of customers in Paris.

CLOSE DATABASES
OPEN DATABASE (HOME(2) + 'Data\testdata')
USE customer  && Opens Customer table

CLEAR
COUNT FOR UPPER(city) = 'PARIS'
DISPLAY FIELDS company, contact FOR UPPER(city) = 'PARIS'

See Also

CALCULATE | SET DELETED | SET TALK