However, two potential drawbacks are that corrupted link list files can be difficult to rebuild in the correct order. Also, there is no way to identify the order in which linked records are added to the file because of the way the system handles deleted records in linked list files.
Linked files are most useful for situations where you wish to attach a group of records to a master record, and the only way you will ever want or need to access those records is through the master record. An example usage is a customer maintenance program that allows the user to keep notes (up to 15 lines of 50 characters each) about each customer.
The following terms will be used to describe the operation and structure of linked files:
Linked file - A file that is opened LINKED, INTERNAL. This file will ultimately contain one or more linked lists.
Linked list - An entire set of linked records. A single linked file will usually contain several linked lists.
Linked list record - Any record in a linked list, regardless of its position within that list. The first eight bytes of each linked listrecord are reserved for two fields: the next record pointer (positions 1-4) and these previous record pointer (positions 5-8). These are maintained by Business Rules! and must not be overwritten by the program. While they are normally ignored, the program can access them with a statement such as the following:
The following Business Rules! instructions play important roles in the use of linked files:READ #1,USING "FORM 2*BH 4":NEXTPTR,PREVPTR
Next record pointer - Positions 1 through 4 of each linked list record, which identify the next record in the linked list. If this pointer is set to 0, the record is the last one in the linked list. This pointer position is maintained by Business Rules! and must not be overwritten by the program.Previous record pointer - Positions 5 through 8 of each linked list record, which point to the previous record in the linked list. If this pointer is set to 0, the record is the first one (the anchor record) in the linked list. This pointer position is maintained by Business Rules! and must not be overwritten by the program.
Anchor record - The first record in a linked list, and the basis for accessing linked list data from the master record. The record number of this anchor point should always be written out to the master file after the list is updated. (If an anchor record has been processed, KREC will return the record number of the anchor record for the current linked list, even if the file pointer is positioned at another record in that list.) The previous record pointer (positions 5-8) of an anchor record will always be set to 0.
Related record - The second and additional records in a linked list. The last record in a linked list will have a next record pointer of 0.
OPEN internal statement - OPEN internal now accepts the LINKED keyword, which specifies that the file to be opened consists of linked lists. For further details see "Open internal" in the Statement section.
RESTORE file statement - In linked files, the RESTORE file statement plays two important roles. First, it can be used with the REC= parameter to reposition the file pointer to any specified record number. The second role of RESTORE is that it can be used without parameters to create a new anchor point for a linked list that you wish to add or insert. For further details see "Restore file" in the Statement section.
READ file statement - The READ file statement now accepts the LINK=string parameter for verifying that linked records belong to a master. It can be used only on LINKED files that have been opened using the KPS= and KLN= parameters. For further details see "Read file" in the Statement section.
WRITE statement - The WRITE statement may be used to insert a record into an existing linked list. (A linked list is not considered to be "existing" unless an anchor point has been established for it with a RESTORE statement.) For further details see "Write" in the Statement section.
DELETE statement - The DELETE statement may be used to delete any anchor record or subrecord from a linked list. If REC= is not specified, the DELETE statement must be preceded by a successful READ of the record to be deleted. For further details see "Delete" in the Statement section.
KREC(filenbr) function - KREC will return the record number of the most recently processed (read or written) anchor record. For further details see "Krec(filenbr)" in the Function section.
Additional "linked file" informatin may be found under the following topics in the Statements section:
Open Internal
Read File
Restore File
Write
LINKED ERROR CODES - The following error codes are specific to LINK for full description see the error codes chapter in your manual. If you need a new listing please contact ADS.
Error 4282 - Data does not match LINK=
Error 4283 - Error in updating previous/next link
Error 0606 - Invalid element in OPEN
Error 0702 - File not opened KEYED or LINKED
Error 0718 - Key length conflict