Using KSAM/XL and KSAM 64 (32650-90886)

206 AppendixA
COBOL Intrinsics
CKREWRITE
If no key in the record is changed, the record pointer continues to point to the current
record. Only a subsequent CKREAD advances the pointer to the next record in the duplicate
key chain. In this case, you can issue CKREAD and CKREWRITE calls until all records with the
duplicated key value have been rewritten.
If any key in the record is changed, the new key is written to the end of the chain of
duplicate keys in the index area. After the first call to CKREWRITE, the record pointer points
to the record whose key value follows the changed key. Since this key is now at the end of
the chain of duplicate keys, a subsequent call to CKREWRITE skips all records with keys in
the duplicate key chain and rewrites the record with the next higher key value. In this
case, you must precede each call to CKREWRITE with a call to CKSTART or CKREADBYKEY in
order to update all subsequent records with duplicate keys.
If you are updating a primary key value that is duplicated, it is good practice to use
CKDELETE to delete the selected record and then rewrite it as a new record with CKWRITE.
When the file is opened in random or dynamic mode (access mode = 1 or 2), no prior call to
a read procedure is needed. You specify the record to be written in WORKING-STORAGE
and then call CKREWRITE. However, you must use the primary key to position to the record
to be modified. When the procedure is executed, the file is searched for a record whose
primary key matches that of the record to be written. If such a record is found, it is
replaced by the record specified in CKREWRITE. If not found, an invalid key condition is
diagnosed and
status
is set to 23.
A call to CKREWRITE in random mode updates only the first record with a key in the chain
of duplicate keys.
Regardless of the mode, after any call to CKREWRITE that does not modify a key value, the
record pointer is positioned to the key of the record just modified. However, if any key in
the modified record was changed, the record must be deleted and then rewritten by a write
procedure. If the access mode is sequential and a key was modified, the pointer is moved to
the record with the next key value in ascending sequence after the modified key. If the
access mode is random or dynamic, and a key was modified, the pointer is moved to the
record with the next key in ascending sequence after the
primary
key in the modified
record. This means that in random or dynamic mode the key pointer may change if it was
pointing to an alternate key before the call to CKREWRITE.
If the file was opened for shared access with CKOPENSHR, then you must lock the file with a
call to CKLOCK before rewriting any records with CKREWRITE. After the records are
rewritten, you should unlock the file with CKUNLOCK.
To ensure that you are updating the correct record in sequential mode, you should call
CKLOCK before positioning the pointer with CKSTART or CKREADBYKEY, then specify the
sequential calls to CKREAD and CKREWRITE before unlocking the file with CKUNLOCK. This
ensures that no other users change the position of the pointer while you are sequentially
updating the file.
In sequential mode, the invalid key condition exists when the record just read by CKREAD
and the record to be written by CKREWRITE do not have the same primary key value. In
random or dynamic mode, an invalid key condition exists if no record can be found in the
file whose primary key matches that of the record to be written by CKREWRITE. In either
case,
status
is set to the value 23.