What is a cursor

A cursor in a FoxPro DataSession window In FoxPro a cursor is a temporary table, the term is short for "CURrent Set Of Records" and is nothing to do with the arrow that the mouse is moving around the screen. You use an SQL query to create a cursor. Open the Data Session window and run this code in the Command Window:

Select Cust_ID ;
   From customer ;
   Into Cursor csrCustID ;
   Order By cust_ID

You will see that csrCustID has appeared in the Data Session window and that it looks very similar to the Customer table beneath it. If you were to click on the Customer table then the only difference that you'd see is that the status line at the bottom of the window would show the work area and the size of the table. In general, a cursor behaves very like a table and the next paragraph will explain why.

Where a cursor is stored

The cursor looks like a FoxPro table and that's because it's stored as a FoxPro table. Select the cursor and type ?dbf() in the Command Window to see the name of the dbf file for the current work area. You'll get something like E:\Temp\000052Q1006I.Tmp as the answer. The cursor is being held in dbf format in your Windows Temp folder.

There is no need to do anything about these temporary files, Fox is supposed to delete them when you close the cursor or close FoxPro. The process does sometimes fail but no harm is done. Fox has no record of the temporary file and will not try to reuse it. You can safely delete any files that FoxPro misses.

NOFILTER - reusing a table

Sometimes you do not get a temporary file at all. Sometimes Fox realises that it can satisfy your SQL request by just filtering records from the current table. This optimisation generally gives you a faster response but it can also crash your code if you are expecting to be able to alter the contents of the cursor without affecting its parent table. Use the Nofilter keyword to force Fox into creating a temporary file:

Select Cust_ID ;
   From customer ;
   Into Cursor csrCustID Nofilter ;
   Order By cust_ID

The Nofilter keyword is a recent addition to Visual FoxPro. In earlier versions we had to add a calculated field to the SQL so that Fox was forced to create a new file. Adding a dummy field:

Select Cust_ID, "Dummy" As Dummy ;
   From customer ;
   Into Cursor csrCustID Nofilter ;
   Order By cust_ID

meant that Fox could not get all the data it needed for the cursor by filtering the source table and forced it to create a new file to hold the extra field.

READWRITE - Modifying a cursor

A standard cursor is read-only and this can be an annoyance because it means that you cannot index the table. FoxPro uses byte 28 of a dbf file to say whether or not there is an index file associated with this data table. This byte cannot be set in a read-only cursor so the cursor cannot be indexed. Use the Readwrite option to create a read/write cursor.

Select Cust_ID, "Dummy" As Dummy ;
   From customer ;
   Into Cursor csrCustID Readwrite ;
   Order By cust_ID

As with the Nofilter keyword mentioned above, Readwrite is a new addition to Visual FoxPro and you might see a workaround in older code. The technique was to create the cursor and then to open the cursor's temporary dbf file as though it were a regular FoxPro table:

Select Cust_ID ;
   From customer ;
   Into Cursor csrTemp ;
   Order By cust_ID

*-- Go to a new work area and open the temp dbf again
Select 0
Use Dbf('csrTemp') Again Alias csrCustID