COUNT Command

Example   See Also

Counts table records.

Syntax

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

Arguments

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 online 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 "Understanding Rushmore Technology" in Chapter 15, Optimizing Applications, in the Programmer's Guide.

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 "Understanding Rushmore Technology" in Chapter 15, Optimizing Applications, in the Programmer's Guide.

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 online topic.