DATA DIVISION
DATA DIVISION defines the variables that are used in the program for data processing. It represents the logical structure of the data, laying out how it is stored in memory.
It is optional. However, each section in the DATA DIVISION has a specific logical function within the program. If sections are coded in the program, those should be coded in the same order shown in the syntax.
Syntax -
DATA DIVISION.
[FILE SECTION.
FD|SD file-description-entry...
[data-description-entry...]]
[WORKING-STORAGE SECTION.
data-description-entry...]
[LINKAGE SECTION.
data-description-entry...]
The DATA DIVISION has mainly three sections -
- FILE SECTION
- WORKING-STORAGE SECTION
- LINKAGE SECTION
FILE SECTION
The record structure of the files should be declared in the FILE SECTION, and it contains all logical structures of the files that are used in the program.
Syntax -
[DATA DIVISION.]
[FILE SECTION.]
[FD|SD logical-file-name]
[RECORD CONTAINS ...]
[BLOCK CONTAINS ...]
[LABEL RECORD IS OMITTED|STANDARD|variable1]
[DATA RECORD IS file-record]
[RECORDING MODE IS F|V|U|S].
01 file-record PIC X(max-rec-length).
Parameters -
- FD - FD is used to specify the file description entry for physical file. SD is used to specify the sort description entry for work files. FD|SD and SELECT clauses should have the same logical-file-name.
- RECORD CONTAINS - Specifies the size of the records.
- BLOCK CONTAINS - Specify the block size in terms of characters or records per block.
- LABEL RECORD IS - Specifies if label records exist in the file or not and their type. (e.g., STANDARD, OMITTED).
- DATA RECORD IS - Assigns a local name to the file record. (e.g., EMPLOYEE-RECORD).
- RECORDING MODE IS - Specifies how the records are stored in the file. (e.g., F(Fixed), V(Variable), U(Unformatted), S(Spanned)). It is only applicable for sequential files.
- 01 file-record ..- Specifies the record structure declaration.
For example - Declaring a file with length 80.
----+----1----+----2----+----3----+----4----+
DATA DIVISION.
FILE SECTION.
FD file1
RECORD CONTAINS 80 CHARACTERS
BLOCK CONTAINS 800 CHARACTERS
RECORDING MODE IS F
DATA RECORD IS emp-rec.
01 emp-rec PIC X(80).
WORKING-STORAGE SECTION
WORKING-STORAGE SECTION defines all the variables and record structures that are required for the processing of data in the program. It defines the variables with their type and size and initializes them with values if needed for processing.
Syntax -
[DATA DIVISION.]
[WORKING-STORAGE SECTION.]
[record-layout-definition.]
[variables-declaration.]
Examples -
Scenario1 - Declaring a variable.
WORKING-STORAGE SECTION.
01 WS-VAR PIC 9(03).
LINKAGE SECTION
LINKAGE SECTION is used to pass data between COBOL programs and receive data from run JCL.
Syntax -
----+----1----+----2----+----3----+----4----+----5----+
DATA DIVISION.
LINKAGE SECTION.
variable-declaration-entries.
Examples -
Scenario1 - Receiving variables from MAINPROG.
DATA DIVISION.
LINKAGE SECTION.
01 LN-VAR.
05 LN-IP1 PIC 9(02). *> To receive input1 from MAINPROG
05 LN-IP2 PIC 9(02). *> To receive input2 from MAINPROG
05 LN-RESULT PIC 9(04). *> To send result to MAINPROG