Commands and Statements

Command SET INDEX Foundation

Opens one or more index files in the current work area.

SET INDEX TO [<cIndexFiles,...>] ;
    [ORDER [TAG] <nIndex> | <cTagName>
    [ASCENDING | descending]
thread-local, current work area
<cIndexFiles,...> is a comma separated list containing the names of the index files to open in the current work area. The names can be specified either as literal file names or as character expressions in parentheses. When the file names are specified without file extensions, the default extension predetermined by the current database engine (DBE) is used. The default extension for index files managed using NTXDBE is ".NTX".
<nIndex> is a positive integer specifying the ordinal position within the current work area of the index to set as the controlling index. Indexes are numbered in the order they were opened using SET INDEX TO or DbSetIndex(). When the argument is missing, the value zero is assumed.
Instead of the numeric ordinal, the tag name of an index can be used. It corresponds with the character string specified for the TAG option of the INDEX command. It can be specified as either a literal or a character expression in parentheses.
The option ASCENDING opens the index in ascending order. This is the default setting.
The option DESCENDING opens the index in descending order. See the function DbSetDescend() for more details.
When the option ADDITIVE is used, all open index files remain open in the work area. Otherwise they are closed before the new index is opened.

The command SET INDEX opens a list of index files in the current work area. If index files are already open in the work area, they are first closed. The first index file in the list <cIndexFiles,...>determines the controlling index and the record pointer is positioned to the first logical record of this controlling index.

Opening index files logically sorts the records of the database open in the work area in the order specified by the controlling index.

If index files are already opened in the current work area and more indexes are to be opened without closing the existing ones, the function DbSetIndex() or the function OrdListAdd() should be used. The controlling index is defined using the command SET ORDER.

The command SET INDEX is often used in multi-user applications instead of the INDEX option of the USE command. This allows the database to be first opened with USE, and the applicable index files are opened only if the database is successfully opened.


// The example shows the use of SET INDEX with literal 
// file names as well as with character expressions. 

   LOCAL cIndexA := "CustA" 
   LOCAL cIndexB := "CustB" 

   USE Invoice NEW 

   USE Customer NEW 
   SET INDEX TO (cIndexA), (cIndexB) 

Open index files in a network environment
// The example shows how the failed opening of a DBF file 
// in a network operation can be intercepted. A message 
// is output and the user makes a decision about retrying the. 
// open operation.  The index files are opened separately if 
// the file is successfully opened in the work area. 


   DO WHILE .T. 
      USE Customer SHARED NEW 

      IF Neterr() .AND. ; 
         Alert( "File is locked!;Access again?", ; 
                {"Retry","Cancel"} ) <> 2 


   IF Used() 
      SET INDEX TO CustA, CustB, CustC 


If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.