Module 10: File processing
FILE ALLOCATION : Step 1 of File Handling
- Files which we want to use in our program should be declared in FILE-CONTROL paragraph of NPUT-OUTPUT SECTION under ENVIRONMENT DIVISION.
- All files used in program must be specified in FILE-CONTROL paragraph.
- SELECT statement is used to identify a file in COBOL program to be associated with external dataset
- Each file must be specified using separate SELECT statement.
- SELECT statement is used to map file with JCL DDNAME
- Basic syntax:-
(To be coded in ENVIRONMENT DIVISION-> INPUT-OUTPUT SECTION -> FILE-CONTROL) SELECT [OPIONAL] file-name AAIGN TO DDNAME [ORGANIZATION IS {SEQUENTIAL/INDEXED/RELATIVE}] [ACCESS IS {SEQUENTIAL/RANDOM/DYNAMIC}] [RECORD KEY IS key-name] [RELATIVE KEY is rel-key] [ALTERNATE RECORD KEY IS alt-key {WITH/WITHOUT} DUPLICATES] [FILE STATUS IS file-status] [RESERVE number AREA]
- While coding, SELECT clause may appear in Margin A, all other clause must appear in Margin B Let’s understand each part of above given syntax:-
- OPTIONAL clause:-
- When coded, it is used to indicate that the file need not mandatorily present when the program runs
- This clause can be coded only for files opened in INPUT, I-O or EXTENDED mode
- If OPTIONAL not coded, then input file Is expected to be present in JCL, otherwise there will be an execution error
- If OPTIONAL is coded and file is not mapped in JCL the file is considered empty and the first read results in end-of-file.
- ASSIGN TO clause:-
- The ASSIGN TO clause associates the name of the file in a program with actual external name of the data file
- file-name in above mentioned syntax indicates logical name used in the COBOL program and DDNAME is logical name in JCL mapped with mainframe dataset
- file-name can be formed by prefixing DDNAME with ‘UT-S-’ to indicate PS(QSAM) file, ‘UT-AS-‘ to indicate ESDS file and no prefix to indicate KSDS/RRDS file
- For example:-
JCL:-//INPUTFL DD DSN=DEPT1.EMP1.DATA1,DISP=SHRCOBOL:-SELECT INFILE ASSIGN TO UT-S-INPUTFL.
- ORGANIZATION clause:-
- The ORGANIZATION clause is used to specify the logical structure of the file. This is established when a file is first created and may not be changed
- It can be coded SEQUENTIAL (for PS or ESDS file), INDEXED( for KSDS file), RELATIVE (for RRDS file). You can understand difference between KSDS, ESDS and RRDS here <please add LINK to VSAM KSDS vs ESDS vs RRDS differences page>
- If ORGANIZATION is not coded, then SEQUENTIAL organization is implied
- ACCESS clause:-
- The ACCESS clause is used to specify the order in which records are read or written.
- It can be coded SEQUENTIAL, RANDOM or DYNAMIC. If ACCESS is not coded, then SEQUENTIAL is implied
- ACCESS IS SEQUENTIAL
- The records are accessed sequentially according to the organization of the file
- When used with ORGANIZATION IS SEQUENTIAL, records in the file are accessed in sequence established when file is created or extended.
- When used with ORGANIZATION IS INDEXED, records in file are accessed in the sequence of ascending records key values according to the collating sequence of the file
- When used with ORGANIZATION IS RELATIVE, records in files are accessed in ascending sequence of relative record number of existing records in the file
- In sequential access, to read 50th record, first 49 records need to be read and skipped
- ACCESS IS RANDOM
- The records can be accessed randomly using the primary/alternate key of INDEXED file organization or using the relative record number of RELATIVE file organization
- It can be used ONLY with INDEXED, RELATIVE file organization
- Example: - In INDEXED file 50th record can be read after getting address of the record from INDEX. In RELATIVE files,, 50th record can be read directly using relative record number
- ACCESS IS DYNAMIC
- This is mixed mode of access. The records can be accesses in random as well as sequential order.
- It can be used ONLY with INDEXED and RELATIVE file organization
- Example:- If you want to read records between 100-150. First randomly read 100th record and then sequentially read till 150th START and READ NEXT commands can used to achieve this
- RECORD KEY clause:-
- The RECORD KEY clause is used to specify the primary key of INDEXED organization file (i.e. KSDS file)
- The value contained in Primary key data item must be unique among the records and part of index record structure
- It can be used with INDEXED file organization
- RELATIVE KEY clause:-
- The RELATIVE KEY is used to specify the data name that specifies the relative record number for a specific logical record within a RELATIVE file organization (i.e. RRDS file)
- It can be used ONLY with RELATIVE file organization
- ALTERNATE RECORD KEY clause:-
- The ALTERNATE RECORD KEY clause is used to specify a data item within the record that provides alternative path to the data in an INDEXED file organization (i.e. KSDS file)
- It can be used only with INDEXED files (KSDS) defined with Alternate INDEX (AIX). We add WITH DUPLICATES clause if Alternate INDEX is defined with duplicates.
- FILE STATUS clause:-
- The FILE STATUS clause is used to monitor the execution of each I/0 operation for the file
- When FILE STATUS clause specified, the system moves a value into ‘file-status’ after each I/O operation. The value in ‘file-status’ can be used to determine next action in program
- ‘file-status’ variable can be defined as PIC X(02) in working storage
- RESERVE clause
- The RESERVE clause is used to specify the number of input/output buffers to be allocated at run time for the files
- If RESERVE clause is not coded, the number of buffers taken from DD statement. If not coded in DD statement then the system default(usually 2) is taken