How to Use SCAN...ENDSCAN to Move Data Between Tables

ID: Q131120

The information in this article applies to:

SUMMARY

SCAN...ENDSCAN is a FoxPro loop construction that performs a user-defined segment of code for each record in the table of the current work area. This article gives examples that illustrate DO WHILE and SCAN...ENDSCAN constructions, which are functionally identical.

MORE INFORMATION

Example of DO WHILE

   GO TOP
   DO WHILE NOT EOF()
     ** some lines of code
     SKIP
   ENDDO

Example of SCAN...ENDSCAN

   GO TOP
   SCAN
     ** some lines of code
   ENDSCAN

Using SCAN...ENDSCAN to Move Data Between Tables

SCAN...ENDSCAN is especially useful for copying the contents of a field in one table into an empty field in another table.

Here is an example scenario. A table named CUSTOMER contains personal data including customer number, name, address, phone number, and so on. A table named NEWCUST contains the same information but its customer number (CNO) field is empty, and it has more current phone numbers and addresses for each of the customers. The CUSTOMER table has the customer numbers in its CNO field. You want to move the CNO data from the CUSTOMER table to the more current NEWCUST table. You are certain that both tables have exactly the same customers sorted in exactly the same way. Therefore, you can just move the CNO field from CUSTOMER into NEWCUST row by row.

The following lines of code illustrate how to use SCAN...ENDSCAN to do it:

   *** Begin SCAN...ENDSCAN example ***
   SELECT newcust
   GO TOP
   SELECT customer
   GO TOP
   SCAN
      SELECT NEWCUST
      REPLACE cno WITH customer.cno
      SELECT customer
   ENDSCAN
   *** End SCAN..ENDSCAN example ***

NOTE: Take special care when doing something like this. Make sure each record in the first table matches the corresponding record in the second table.

Additional reference words: FoxWin FoxMac FoxDos 2.00 2.50 2.50a 2.50b 2.50c 2.60 2.60a match both post populate KBCategory: kbprg kbcode KBSubcategory: FxprgTable

Last Reviewed: June 27, 1995