Question: If I want to read a KSDS dataset using CICS and I know only part of a key, do I need to first use the STARTBR command and then the READNEXT command? If I know the full key do I need to use STARTBR or is a READ command enough?
Answer: Whether you use STARTBR and READNEXT or alternatively use the READ command depends not on how much of the key that you know but on whether or not you intend reading more records following the first record that matches your key.
If you are just trying to retrieve one record then you can use the READ command to retrieve the record. If you don't know the complete key then use the KEYLENGTH parameter to specify the length of part of the key that you do know and the GENERIC parameter to tell the READ command to retrieve the first record where the specified key matches the leftmost characters of the key (the length that will be matched is what you specified in keylength). Where you do know the complete key you wont need to specify either of these parameters and the whole key will be matched.
If after reading this record you then intend to read further records either immediately following or preceding the record then you should use the STARTBR command to start browsing the file and then the READNEXT or READPREV commands to read records from the file. The STARTBR command positions you in the file ready to read the first record. If you only know part of the key you can use the KEYLENGTH and GENERIC parameters with the STARTBR command in order to set your initial position correctly or alternatively initialize the rest of the key to low values and use the GTEQ parameter (which is a default) to let the browse position itself at the first record having a key greater than or equal to the one specified. Once you have started the browse you can then use the READNEXT and READPREV commands to read forwards and backwards through the file. Once you are finished reading you need to use the ENDBR command to close the browse.
This article written by Stephen Chapman, Felgall Pty Ltd.