WITH DUPLICATES on primary keys

In HP COBOL compatibility mode, duplicate primary key values are allowed if the indexed file system supports them and the WITH DUPLICATES phrase is specified in the File-Control paragraph. Vision and KSAM support duplicate primary key values. The syntax is:

RECORD KEY IS key-name [= seg-name] [WITH [NO] DUPLICATES]

The phrase WITH NO DUPLICATES is commentary because, by default, duplicate values are not allowed for the primary key.

The phrase WITH DUPLICATES indicates that duplicate primary key values are allowed. If WITH DUPLICATES is used with a file system that doesn't support duplicate primary keys, when the file is created via the OPEN statement a status of 0M is returned, indicating that the file was successfully created but that duplicate primary keys are not supported.

When WITH DUPLICATES is used with Vision, KSAM, and other file systems that support it, the rules that govern how REWRITE and DELETE operations are handled are determined by the file system. This is because support for duplicate primary keys is not part of the ANSI 1985 Standard, few files systems support it, and therefore no common rules exist. The rules for Vision are given in General Rule 14 of File-Control Paragraph.

There are some small but important differences between how KSAM and Vision handle REWRITE and DELETE when duplicate primary keys are allowed.

Under KSAM, the first record is rewritten or deleted unless the file is open with ACCESS SEQUENTIAL mode. These latter programs should convert easily to Vision because Vision's default lock handling will cause the correct record to be locked and result in the same set of records being updated.

For RANDOM and DYNAMIC access files, there could be a difference between how KSAM and Vision update records depending on how the records are read before they are rewritten or deleted. Note that the HP COBOL II Reference Manual advises against allowing duplicates primary key values with RANDOM or DYNAMIC access files. KSAM's behavior can be mimicked with Vision by always reading the record immediately before rewriting or deleting it. This ensures the correct record is accessed. Otherwise, KSAM and Vision rules are generally compatible unless the program does a series of READ NEXT's and then rewrites or deletes a record other than the one last read.