Access to segments through a secondary index allows a program to process segments in an order that is not the physical sequence of the database. The order segment in Figure 7-6 on page 76 is a good example of how secondary indexes can be used. To process an order when only the customer order number is known, the ORDER segment can be accessed by using the customer order number. This is the simplest form of a secondary index. Another basic use for a secondary index is to provide a method of processing a subset of the segments in a database without having to read the entire database. An example of this would be to provide a secondary index on a Balance-owing field in the customer database. The secondary index database could be defined to only contain those database records for which a non-zero balance is owing. Accessing Segments by Using a Secondary IndexThe format of the CALL statement parameters for accessing segments through a secondary index are identical to those used to access segments through the primary path. The difference is in how the PCB is coded in the PSB. The second PCB in the PSB in Figure 15-14 shows how to define a PCB that can be used to access a segment through a secondary index. Figure 15-14. Example of a PSB with a Secondary Index Defined* * PSB with Secondary index PCB * PCB TYPE=DB,PROCOPT=G, DBDNAME=BE2CUST,,KEYLEN=6 PCB TYPE=DB,PROCOPT=G, DBDNAME=BE2CUST,,PROCSEQ=FE2CNAM,,KEYLEN=20 * SENSEG NAME=SE2PSCUST PSBGENG,LANG=COBOL,PSBNAME=SE2PCUST,CMPAT=YES END Retrieving Segments Using a Secondary IndexThe same calls are used as described in "Retrieving Segments" on page 248. However, the index search field, which is defined by an XDFLD statement in the DBD, is used in the SSA for the get unique call of the root segment. The index search field defines the secondary processing sequence. After the successful completion of this get unique call, the PCB and IOAREA look the same as after the basic get unique call in Figure 15-2 on page 248, except that the key feedback area now starts with the Customer name field. When using the secondary processing sequence, consecutive get next calls for the CUSTOMER ORDER segment present the CUSTOMER ORDER segments in Customer name sequence. If both the primary and the secondary processing sequence are needed in one program, you should use two PCBs, as shown in Figure 15-15. Figure 15-15. Example of a Get Unique Call Using a Secondary Index77 GU-FUNC PICTURE XXXX VALUE 'GU 01 SSA002-GU-SE2PCUST. 02 SSA002-BEGIN PICTURE x(19) VALUE 'SE2PCUST(FE2PCNAM='. 02 SSA002-FE2PCNAM PICTURE X(20). 02 SS1002-END PICTURE X VALUE ')'. 01 IOAREA PICTURE X(256). -------------------------------------------------------------------------- MOVE CUSTOMER-NAME TO SSA002-FE2PCNAM. CALL 'CBLTDLI' USING GU-FUNC,PCB-NAME,IOAREA,SSA002-GU-SE2PCUST. -------------------------------------------------------------------------- STATUS CODES: ------------- : succesfull call GE: exceptional but correct condition other: error condition Replacing Segments Using a Secondary IndexTo replace segments in the indexed database, you can use a combination of get hold and replace calls, but you cannot change sequence fields. However, you can change index search fields. If you change an index search field, DL/I automatically updates the index database by deleting the old pointer segment and inserting a new pointer segment. Note: When you use a secondary processing sequence, you might need to access the database record again later. Deleting Segments Using a Secondary IndexWhen using a secondary processing sequence, you cannot delete the index target segment (the root segment that depends on the definitions). If you must delete the index target segment, use a separate PCB with a primary processing sequence. Inserting Segments Using a Secondary IndexWhen you use a secondary processing sequence, you cannot insert the index target segment. In all other cases, the insert call will work as described in "Inserting Segments" on page 254. Creating Secondary IndexesYou can create a secondary index during the initial load of the indexed database, or later. The secondary index database is created by the DL/I reorganization utilities. No application programs are required. |