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 default scope for COUNT is ALL records.

    The scope clauses are: ALL, NEXT nRecords, RECORD nRecordNumber, and REST. Commands that include Scope operate only on the table in the active work area.

    For more information on scope clauses, see Scope Clauses.

  • 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 Query Optimization 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 Command and Using Rushmore Query Optimization 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 Command and Using Rushmore Query Optimization 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 Behavior of Null Values in Commands and Functions 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

Reference

CALCULATE Command

SET DELETED Command

SET TALK Command

Other Resources

Commands (Visual FoxPro)

Language Reference (Visual FoxPro)