Command PACK Foundation
Physically removes records marked for deletion from a table.
PACK [DBF][MEMO] [IN <nWorkArea> | <cAlias> ] | [cFileSpec]
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.
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.
// 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
// 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
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.