Command PACK Foundation

Physically removes records marked for deletion from a table.

Syntax
PACK [DBF][MEMO] [IN <nWorkArea> | <cAlias> ] | [cFileSpec]
Parameters
DBF | MEMO
This command option is ignored in Xbase++, it is an implementaton detail of the DatabaseEngine if records and or memo blocks get packed.
IN <nWorkArea>
<nWorkArea> is a positive integer specifying the ordinal number of the work area being selected.
IN <cAlias>
This argument is either a literal alias name or a character expression containing the alias name in parentheses. The alias name indicates the work area whose files are to be closed.
<cFileSpec>
The <cFileSpec> clause is ignored in Xbase++ 2.0
Description

The file command PACK physically removes all records marked for deletion from a file open in the current work area or the workarea specified with the IN clause. Open index files are updated and the storage space of the deleted records on the hard disk or diskette is released. The records are physically removed from the file and can no longer be "undeleted" using the command RECALL. The command PACK does not provide a backup copy of the file in which records are deleted. After PACK terminates, the record pointer is positioned on the first logical record in the work area.

Memo files are not affected by the command PACK. To release unused storage space in a memo file, the command COPY TO must be used. COPY TO copies records in a file into a temporary file. The original data and memo files (DBF and DBT file) can then be deleted using ERASE and the temporary DBF and DBT files renamed to the original filenames.

In multi-user applications on a network, the file must be exclusively opened before the command PACK can be executed. Otherwise, a runtime error occurs.

When all records in a work area are to be deleted at one time, the command ZAP should be used.

The PACK command can take a considerable amount of time, especially for large files containing many deleted records. For this reason, a strategy that recycles records should be considered. A recycling strategy can offer a significant speed advantage in applications with a high transaction rate.

Examples
PACK
// The example shows the effect of the PACK command after 
// several records have been marked for deletion. 

PROCEDURE Main 

   USE Customer NEW 
   ? LastRec()                // result: 100 

   DELETE NEXT 5 
   PACK 
   ? LastRec()                // result: 95 

   CLOSE 

RETURN 
Data recycling with RECALL
// In the example, an index expression is created that groups 
// all deleted records at the end of the file. When a 
// new record is added to the file, the UDF MyAppend() 
// first tests whether a record with a deletion flag 
// is available. If one is available, the deleted record is 
// recalled and the record is filled with empty values. 
// Using this technique, the time consuming PACK command can 
// be avoided. 

PROCEDURE Main 
   USE Customer NEW EXCLUSIVE 

   INDEX ON IIf(Deleted(),Chr(255)+SubStr(NAME,2),NAME) ; 
         TO CustA 

   SET INDEX TO CustA 
   SET DELETED ON 

   ? LastRec()             // result: 100 
   APPEND BLANK 

   ? LastRec()             // result: 101 

   DELETE 
   ? LastRec()             // result: 101 

   MyAppend() 
   ? LastRec()             // result: 101 

   CLOSE Customer 
RETURN 

PROCEDURE MyAppend() 
   LOCAL aData, nIndex 

   GOTO  LastRec()          // go to the last physical 
                            // record 
   IF Deleted()             // Is deletion flag set ? 
      nIndex := IndexOrd()  // save current index 
      SET ORDER TO 0        // deactivate controlling index 

      GOTO  LastRec()       // copy empty data from the 
      SKIP                  // "ghost-record" to an array 

      aData := Array( FCount() ) 
      AEval( aData, {|x,i| aData[i]:=FieldGet(i)} ) 

      GOTO  LastRec()       // back to the Deleted() record 
      RECALL                // remove deletion flag 
                            // copy empty data to record 
      AEval( aData, {|x,i| FieldPut(i,x)} ) 
      SET ORDER TO nIndex   // reactivate controlling index 
      GOTO  LastRec()       // update index files 
   ELSE                     // no record available with 
      APPEND BLANK          // deletion flag, append new record. 
   ENDIF 

RETURN 
Feedback

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.