Deletebykey

This instruction deletes a record at a given key after checking for edit conflicts. It compares the UpdTick value of the [F] record with the UpdTick of the corresponding database record and it deletes the database record only if the two ticks are equal.

This instruction allows CRUD operations to work safely in a multi-user environment without locking resources. The operation fails if the database record was modified after the [F] record had been read.

The fstat variable is set by this instruction. A status other than 0 indicates a failure.

Syntax

DeleteByKey [ABV]KEY = KEY_VALUEDeleteByKey KEY = KEY_VALUEDeleteByKey [ABV]KEY =DeleteByKey KEY =DeleteBykey =

All these elements are optional.

Example

# Procedure that deletes a given customer record only if the customer is not activeSubprog DELETE_INACTIVE_CUSTOMER(CUST_CODE, ERR_CODE, ERR_MESSAGE)Const Char CUST_CODE()Variable Integer ERR_CODEVariable Char ERR_MESSAGE()# Declare the tableIf !clalev([BPC])Local File BPCUSTOMER [BPC]Endif# Read the recordRead [BPC]BPCNUM=CUST_CODEIf fstat : ERR_CODE=fstat : ERR_MESSAGE="Customer"-CUST_CODE-"not found" : End : Endif# Check if inactiveIf [BPC]BPCSTA=2 : ERR_CODE=8 : ERR_MESSAGE="Customer"-CUST_CODE-"still active" : End : Endif# Delete the recordDeleteByKey [BPC]BPCNUM=CUST_CODE# Check the fstat valueERR_CODE=fstatCase ERR_CODEWhen 0 : ERR_MESSAGE="Operation succeeded"When 1 : ERR_MESSAGE="Customer record is locked"When 6 : ERR_MESSAGE="Customer record was modified"When 7 : ERR_MESSAGE="Customer record does not exist"EndcaseEnd

fstat values

ValueExplanation
0Operation succeeded.
1Record is locked.
6Update conflict.
7Record does not exist.

See also

Updtick, RewriteByKey, File, fstat