You are on page 1of 44

MAINFRAME MATERIAL - COBOL, DB2, AND CICS

AUTHOR - K PHANI KUMAR,

Contents
Contents...................................................................................................................................................................1
COBOL - COMMON BUSINESS ORIENTED LANGUAGE ..............................................................................3
DIVISIONS........................................................................................................................................................3
COBOL CODING SHEET..................................................................................................................................5
DATA TYPES....................................................................................................................................................6
LEVEL NUMBERS ...........................................................................................................................................6
SPECIAL LEVEL NUMBERS ...........................................................................................................................7
MOVE VERB ....................................................................................................................................................8
ADD VERB .......................................................................................................................................................9
SUBTRACT VERB .........................................................................................................................................10
MULTIPLY VERB ...........................................................................................................................................11
DIVIDE VERB .................................................................................................................................................11
COMPUTE VERB ...........................................................................................................................................12
REDEFINE CLAUSE ......................................................................................................................................12
IF STATEMENT ..............................................................................................................................................13
EVALUATE STATEMENT ..............................................................................................................................14
PERFORM STATEMENTS .............................................................................................................................14
EXIT, STOP RUN, EXIT PROGRAM AND GOBACK .....................................................................................17
SUB PROGRAMS...........................................................................................................................................18
TABLE HANDLING..........................................................................................................................................20
INTERNAL SORT............................................................................................................................................22
FILE HANDLING.............................................................................................................................................23
DB2 - DATABASE 2 .......................................................................................................................................28
COMPILATION PROCESS ............................................................................................................................36
INCLUDE and COPY statements....................................................................................................................39
1

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,
JOINS .............................................................................................................................................................39

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

COBOL - COMMON BUSINESS ORIENTED LANGUAGE


COBOL stands for COMMON BUSINESS ORIENTED LANGUAGE. COBOL Programs are
used for commercial data processing. COBOL is an English like language.
DIVISIONS
A COBOL program consists of 4 divisions.

IDENTIFICATION DIVISION
ENVIRONMENT DIVISION
DATA DIVISION
PROCEDURE DIVISION

IDENTIFICATION DIVISION: Its used to identify the COBOL program.


It tells the system what the program name is, who has written it, when it was
compiled etc.
IDENTIFICATION DIVISION.
PROGRAM-ID.
CR25410.
AUTHOR.
PHANI.
INSTALLATION. WIPRO.
DATE-WRITTEN. 12-12-2005.
ENVIRONMENT DIVISION: Its used to tell the system what is the source compute and object computers
used in the programs.
And also what are the input and output files used in the program.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. 3270.
OBJECT-COMPUTER. 3278.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT CNTLCARD-FILE
ASSIGN TO CARDFILE.
SELECT CHARGEBACK-CONTROL-FILE
ASSIGN TO CBUCCF
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS CCF-RECORD-KEY-FD
FILE STATUS IS STATUS-IND.
3

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

CONFIGURATION SECTION gives the source and object computer details. i.e.
it gives the information about where the program has been writer(Terminal)
and where it can be executed. This section is optional now.
INPUT-OUTPUT SECTION gives the input and output files that the program is
going to be used. Whatever the files that we use in COBOL program that has
to be declared in the JCL.

DATA DIVISION: Its used to declare the variables used in the COBOL program, and to specify
the file attributes, and to pass the data from one program to another
program. It contains FILE SECTION, WORKING-STORAGE SECTION and
LINKAGE-SECTION.

FILE-SECTION - If we use any files in the COBOL program that has to be


declared here.

WORKING-STORAGE SECTION - Its used to declare all the variables used in


the COBOL program.

LINKAGE SECTION - Its used to pass the data from main program to sub
program.

DATA DIVISION.
FILE SECTION.
FD CNTLCARD-FILE
BLOCK CONTAINS 0 RECORDS
RECORD CONTAINS 80 CHARACTERS.
LABEL RECORDS ARE STANDARD
DATA RECORD IS CNTLCARD-REC.
01 CNTLCARD-REC
PIC X(80).
WORKING-STORAGE SECTION.
77 RECORDS-READ
PIC 9(7)
77 RECORDS-SELECTED
PIC 9(7)
LINKAGE SECTION.

VALUE 0 COMP-3.
VALUE 0 COMP-3.

PROCEDURE DIVISION: Its used to write the logic of the program. The COBOL instructions start from
here. And also the execution of the program begins from the PROCEDURE
DIVISION.

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

IDENTIFICATION DIVISION.
PROGRAM-ID.
HELLOPGM.
AUTHOR.
PHANI.
INSTALLATION. WIPRO.
DATE-WRITTEN. 12-12-2005.
ENVIRONMENT DIVISION.
DATA DIVISION.
PROCEDURE DIVISION.
A-START.
DISPLAY HELLOW WORLD OF MAINFRMES.
STOP RUN.
COBOL CODING SHEET

Every COBOL program has to be written in a 80 columns and 24 rows PDS


member.
Columns 1 to 6 will be used by the user for writing the sequence numbers.
Column 7 is used for indicating
o * Comment
o _ Continuation
o D Debugging line
Each member will be having two margins.
o Margin-A - It starts from column 8th.
o Margin-B - It starts from column 12th to 72nd.
Margin-A contains the Division names, Section names, Paragraph names,
Level numbers, File description and Sort Descriptions.
Margin-B contains the logic of your program.
Columns 73rd to 80 used by the System for writing the offset address.

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

DATA TYPES

In COBOL we have the following data types.


1. NUMERIC
==> It will be represented using 9
2. ALPHA NUMERIC ==> It will be represented using X
3. ALPHABETIC
==> It will be represented using A

LEVEL NUMBERS

In COBOL each variable has to be declared with a level number.


We have 01 - 49 level numbers in COBOL.
And also special level numbers 66, 77, 88.
Level No - 01 is used to declare the group variable.

Ex6

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

01 WORK-REF-NUM.
05 WORK-REF-NUM-1
05 WORK-REF-NUM-2
05 FILLER
05 WORK-REF-DATE
05 FILLER
05 WORK-REF-19-23

PIC
PIC
PIC
PIC
PIC
PIC

01 WS-NAME.
05 INITIAL
05 SUR-NAME
05 FIRST-NAME

PIC X(1)
PIC X(1)
PIC X(1)

X(1)
X(2)
X(4)
9(4)
X(7)
X(5)

VALUE
VALUE
VALUE
VALUE
VALUE
VALUE

SPACES.
SPACES.
SPACES.
ZERO.
SPACES.
SPACES.

VALUE SPACES.
VALUE SPACES.
VALUE SPACES.

Here 01 is a Group level item and 05 are called as Sub-Level items.


It gives the data-type and the size of the data-item. This specifies,
whether, the storage location stores numbers, alphabets, or alpha-numeric data. It
also shows, how much the width of the data-type is.
PICTURE Clause:

Examples
PICTURE 999 -> Stores a 3 digit +ve no.
PICTURE S999 -> Stores a 3 digit +ve/-ve no.
PICTURE XXXX -> Stores a string of 4 characters
PICTURE 99V99 -> Stores a +ve real from 0-99.99
PICTURE S9V9 -> Stores a +ve/-ve real from 9.9 to +9.9
Shorthand Notation : You can abbreviate 9999 as 9(4), XXXX as X(4), 999V99 as
9(3)V9(2), S99999 as S9(5) etc.
VALUE Clause : We can assign an initial value to the elementary dataitems/variables by using the VALUE clause. It is an optional clause.
01 WS-EMP-SALARY
01 WS-EMP-JDATE

PIC 9(4)V99
PIC X(10)

VALUE 1000.52
VALUE 26-06-09

SPECIAL LEVEL NUMBERS

Special level numbers are 66, 77, and 88.

66 - This level number is used for RENAMES clause.


01 WS-NAME.
05 INITIAL
05 SUR-NAME
05 FIRST-NAME

PIC X(1)
PIC X(15)
PIC X(20)

VALUE SPACES.
VALUE SPACES.
VALUE SPACES.
7

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

66 WS-FULL-NAME RENAMES INITIAL, SUR-NAME, FIRST-NAME.


Now we have two variables WS-NAME, WS-FULL-NAME contains the same
values.
01 WS-INPUT.
05 RECORD-1.
10 FIELD-A
10 FIELD-B
05

RECORD-2.
10 FIELD-C
10 FIELD-D

PIC 9(2) VALUE 10.


PIC 9(2) VALUE 20.
PIC 9(2) VALUE 30.
PIC 9(2) VALUE 40.

66 RECORD-3 RENAMES FIELD-B THRU FIELD-C.


DISPLAY RECORD-3.
O\P - 2030

77 - This level number is used for declaring the individual items. That means there
will be no sub-levels for that variable.s
77
77

INITIAL
SUR-NAME

PIC X(1)
PIC X(15)

VALUE SPACES.
VALUE SPACES.

88 - This level number is used for condition names.


01

STATUS-IND
88 GOOD-STATUS
88
88
88

END-OF-FILE-STATUS
DUP-REC-STATUS
NO-RECORD-FOUND

PIC XX

VALUE SPACES.
VALUES '00'
'97'.
VALUE '10'.
VALUE '22'.
VALUE '23'.

MOVE VERB

MOVE verb is used to move the values from one variable to another variable.
The way MOVE statement work is different for NUMERIC and ALPHABETIC,
ALPHA-NUMERIC variables.
There are different MOVE statements.

Format-1:8

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

MOVE 123 TO A.
MOVE A TO B.

==> Value 123 is stored in the variable A.


==> Value of A is stored in the variable B.

Format-2:MOVE 123 TO A, B. ==> Value 123 is stored in the variable A & B.


Format-3:01 EMP-REC-1.
05 NAME
PIC
05 AGE
PIC
05 SALARY PIC

X(15).
9(3).
9(5)V9(2).

01 EMP-REC-2.
05 NAME
05 AGE
05 SALARY
05 ADDR

X(15).
9(3).
9(5)V9(2).
X(30).

PIC
PIC
PIC
PIC

MOVE EMP-REC-1 CORRESPONDING EMP-REC-2.


NOTE - Both EMP-REC-1 and EMP-REC-2 are having the common variables,
if we want to move the common variables we use the FORMAT-3. It will
move the variables which are common.
ADD VERB

ADD verb is used to add the variables or constants.

Format-1:01 A PIC 9(2)VALUE 25.


ADD 10 TO A.
DISPLAY A.
O\P - 35. ==> Value in A is 25 then it adds 10 to variable A.
Format-2:01 A PIC 9(2)VALUE 25.
01 B PIC 9(2)VALUE 15.
ADD 10 TO A, B.
DISPLAY A.
DISPLAY B.
O\P - 35
25
9

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

Format-3:01 A PIC 9(2)VALUE 25.


01 B PIC 9(2)VALUE 15.
01 C PIC 9(2).
ADD A TO B GIVING C.
DISPLAY A.
DISPLAY B.
DISPLAY C.
O\P - 25
15
40 ==> Here A & B values are not changed and result stored in C
SUBTRACT VERB

SUBTRACT verb is used to subtract the variables or constants.

Format-1:01 A PIC 9(2)VALUE 25.


SUBTRACT 10 FROM A.
DISPLAY A.
O\P - 15. ==> Value in A is 25 then it subtracts 10 from variable A.
Format-2:01 A PIC 9(2)VALUE 25.
01 B PIC 9(2)VALUE 15.
SUBTRACT 10 FROM A, B.
DISPLAY A.
DISPLAY B.
O\P - 15
05
Format-3:01 A PIC 9(2)VALUE 25.
01 B PIC 9(2)VALUE 15.
01 C PIC S9(2).
SUBTRACT A FROM B GIVING C.
DISPLAY A.
DISPLAY B.
DISPLAY C.
10

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

O\P - 25
15
-10 ==> Here A & B values are not changed and result stored in C
MULTIPLY VERB

MULTIPLY verb is used for Multiplication.

Format-1:01 A PIC 9(3)VALUE 25.


MUPLTIPLY 10 BY A.
DISPLAY A.
O\P - 250
Format-2:01 A PIC 9(3)VALUE 25.
01 B PIC 9(3)VALUE 10.
01 C PIC 9(3).
MUPLTIPLY A BY B GIVING C.
DISPLAY A.
DISPLAY B.
DISPLAY C.
O\P - 25
10
250
DIVIDE VERB

DIVIDE verb is used for Division.

Format-1:01 A PIC 9(3)VALUE 25.


DIVIDE 10 INTO B.
DISPLAY A.
O\P - 2
Format-2:01 A PIC 9(3)VALUE 25.
01 B PIC 9(3)VALUE 10.
11

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

01 C PIC
01 D PIC

9(3).
9(3).

DIVIDE A INTO B GIVING C REMAINDER D.


DISPLAY A.
DISPLAY B.
DISPLAY C.
DISPLAY C.
O\P - 25
10
2
5
COMPUTE VERB

For doing large arithmetic operations we cant use the ADD, SUBTRACT,
MULTIPLY and DIVIDE verbs.
So for doing this we can use COMPUTE verb. And also we can use it for
assigning a value to a variable.

ExCOMPUTE A = 100.
COMPUTE A = (B+C)/E.

REDEFINE CLAUSE

REDEFINE clause is used to redefine the same memory location with different
names and also with different data types.

Ex:01

WS-AUG28-TODAY-DATE

PIC 9(7).

01

WS-AUG28-TODAY-REF REDEFINES WS-AUG28-TODAY-DATE.


05 WS-AUG28-TODAY-CENTURY PIC 9(2).
05 WS-AUG28-TODAY-YEAR
PIC 9(2).
05 WS-AUG28-TODAY-DAY
PIC 9(3).
12

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

01

WS-CARD-NUMBER

PIC X(16).

01

WS-CARD-NUMBER-REF REDEFINES WS-CARD-NUMBER.


05 WS-NETWORK-NUMBER
PIC 9(4).
05 WS-REST-OF-CARD-NUM
PIC 9(12).

IF STATEMENT

IF statement is used for decision making in COBOL.

Format-1:IF <CONDITION> THEN


Statement1
Statement2
Statement3
END-IF.
Statement-X.
Here if the condition is true then it will execute the statements1, 2
and 3.
If the condition is false then it will directly execute the
statement-X.
Format-2:IF <CONDITION> THEN
Statement1
Statement2
Statement3
ELSE
Statement4
Statement5
Statement6
END-IF.
ExIF AMT > 0
DISPLAY AMOUNT IS POSITIVE AMT
ELSE
DISPLAY AMOUNT IS NEGATIVE AMT
END-IF.

13

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

EVALUATE STATEMENT

EVALUATE statement is also used for decision making in COBOL. This is like a
CASE statement in C language.

Format-1:ExEVALUATE TRUE
WHEN SQLCODE = 0
DISPLAY DB2 QUERY SUCCESSFULLY COMPLETED
WHEN SQLCODE = 100
DISPLAY ROW NOT FOUND
WHEN SQLCODE = -811
DISPLAY MULTIPLE ROWS FOUND
WHEN OTHER
DISPLAY UNKNOWN ERROR WHILE EXECUTING THE TRANSACTION
END-EVALUATE.
Format-2:ExEVALUATE SQLCODE
WHEN 0
DISPLAY DB2 QUERY SUCCESSFULLY COMPLETED
WHEN 100
DISPLAY ROW NOT FOUND
WHEN -811
DISPLAY MULTIPLE ROWS FOUND
WHEN OTHER
DISPLAY UNKNOWN ERROR WHILE EXECUTING THE TRANSACTION
END-EVALUATE.

PERFORM STATEMENTS

PERFORM statement is used for conditional branching in COBOL. We have 5


different types of PERFORM statements.

Format-1: - In this format the control will go to the specified paragraph and
executes all the statements mentioned in that and returns back to the statement
after the PERFORM.
It calls the Paragraph only one time in this case.
PERFORM B-CALCULATE-SALARY.

14

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

Format-2: - In this format the control will go to the specified paragraph and
executes all the statements mentioned in that UNTIL a specified number of times
and then returns back to the statement after the PERFORM.
It calls the Paragraph 10 Times.
PERFORM B-CALCULATE-SALARY UNTIL 10 TIMES.
Format-3: - In this format the control will go to the specified paragraph and
executes all the statements mentioned in that UNTIL a specified condition is TRUE.
It calls the Paragraph until a specified condition is TRUE.
PERFORM B-CALCULATE-SALARY UNTIL UNTIL END-OF-FILE = YES.
Format-4: - In this format the control will go to the specified paragraph and
executes all the statements mentioned in that UNTIL a specified condition is TRUE.
Here it will maintain a counter and increments it to the value specified for each
iteration.
PERFORM B-CALCULATE-SALARY VARYING CTR FROM 1 BY 1 UNTIL CTR > 10.
In this example the CTR starts from 1 and for each iteration it will
increment with value 1. (BY 1).

15

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

Ex-1:PROCEDURE DIVISION.
100-MAIN-PARA.
DISPLAY START OF PROGRAM.
PERFORM 200-INDIA-PARA.
DISPLAY END OF PROGRAM.
STOP RUN.
200-INDIA-PARA.
DISPLAY INDIA .
O\P INDIA
Ex-2:PROCEDURE DIVISION.
100-MAIN-PARA.
DISPLAY START OF PROGRAM.
PERFORM 200-INDIA-PARA UNTIL 3 TIMES.
DISPLAY END OF PROGRAM.
STOP RUN.
200-INDIA-PARA.
DISPLAY INDIA .
O\P INDIA
INDIA
INDIA
Ex-3:PROCEDURE DIVISION.
100-MAIN-PARA.
DISPLAY START OF PROGRAM.
PERFORM 200-INDIA-PARA THRU 400-USA-PARA UNTIL WS-FLAG = YES
DISPLAY END OF PROGRAM.
STOP RUN.
200-INDIA-PARA.
DISPLAY INDIA .
300-UK-PARA.
DISPLAY UK .
400-USA-PARA.
DISPLAY USA .
MOVE YES TO WS-FLAG.
O\P INDIA
UK
USA
16

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

EXIT, STOP RUN, EXIT PROGRAM AND GOBACK


EXIT: This statement is used to indicate its end of the paragraph.
PROCEDURE DIVISION.
100-MAIN-PARA.
PERFORM 200-INDIA-PARA THRU 400-USA-PARA UNTIL WS-FLAG = YES
STOP RUN.
100-EXIT.
EXIT.
200-INDIA-PARA.
DISPLAY INDIA .
200-EXIT.
EXIT.
STOP RUN: This statement is used to indicate its end of the program.
And the control goes back to the OS.
PROCEDURE DIVISION.
100-MAIN-PARA.
DISPLAY HELLOW IBM MAINFRAMES.
STOP RUN.
100-EXIT.
EXIT.
EXIT

PROGRAM AND GO BACK: This statement is also used to indicate its end of the program.
Must be used in the Sub-programs.
Control will goes back to the main program from where its
called.

PROCEDURE DIVISION.
100-MAIN-PARA.
DISPLAY HELLOW IBM MAINFRAMES.
EXIT PROGRAM.
100-EXIT.
EXIT.

17

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

SUB PROGRAMS

In COBOL CALL is statement is used to transfer the control from


Main program to Sub-programs. There are three ways of calling a
sub-program.
o BY REFERENCE
o BY VALUE
o BY CONTENCT

BY REFERENCE - This is the default one and in this if you change


a variable in the Sub-Program that will be reflected in the Main
Program automatically.
CALL CR2510 USING WS-A, WS-B, WS-C. (OR)
CALL CR2510 BY REFERENCE USING WS-A, WS-B, WS-C.

BY VALUE - You have to specify this explicitly while calling the


sub-program. In this we are just passing the value of a variable
and if any change to that variable in the sub-program that will
not be reflected in the main program.
CALL CR2510 BY VALUE USING WS-A, WS-B, WS-C.

BY CONTENT - You have to specify this explicitly while calling


the sub-program. In this we are just passing the value of a
variable and sub-program is not allowed to change the value of
this variable.
CALL CR2510 BY CONTENT USING WS-A, WS-B, WS-C.

Changes in the Sub-Program:To receive the data passed from main program, the sub-program
has to make the below changes.
o We need to define the No of arguments that we want to
receive in the LINKAGE SECTION of the Sub-program.

LINKAGE SECTION.
18

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

01 DATA.
05 LS-A
05 LS-B
05 LS-C

PIC 9(2).
PIC 9(2).
PIC 9(2).

o PROCEDURE DIVISION USING LS-A, LS-B, LS-C.


Ex:MAIN PROGRAM
IDENTIFICATION DIVISION.
PROGRAM-ID. MAINPGM.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-A
PIC 9(2) VALUE 30.
PROCEDURE DIVISION.
A-START.
DISPLAY 'VALUE OF A BEFORE CALLING SUB PROGRAM' WS-A.
CALL 'SUB-PGM1' USING WS-A.
DISPLAY 'VALUE OF A AFTER CALLING SUB PROGRAM' WS-A.
STOP RUN.
A-EXIT.
EXIT.
SUB PROGRAM.
IDENTIFICATION DIVISION.
PROGRAM-ID. SUB-PGM1.
ENVIRONMENT DIVISION.
DATA DIVISION.
LINKAGE SECTION.
01 LS-A
PIC 9(2).
PROCEDURE DIVISION USING LS-A.
A-START.
MOVE 45 TO LS-A.
EXIT PROGRAM.
A-EXIT.
EXIT.
O\P:VALUE OF A BEFORE CALLING SUB PROGRAM 30
VALUE OF A AFTER CALLING SUB PROGRAM 45
Sub-Programs are called using the below methods.
1. STATIC CALL
19

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

2. DYNAMIC CALL

STATIC CALL: - In Static Call, programs are called as below.


CALL 'SUB-PGM1' USING WS-A.
o In Static Call main program and Sub program will be having the same
load module.
o Programs are called using PGM-NAME. (Embedded in Quotes).
o The compiler option NODYNAM have to be used.
o As the main program and sub programs load module will be in one load
module execution is faster.
Disadvantages:
o If there is a change in one sub-program, all the programs has to be
compiled to get the latest load module.
o The length of the load module is big as it contains all the programs
load modules.
DYNAMIC CALL: - In Dynamic Call, programs are called as below.
MOVE SUB-PGM1
TO WS-PGM-NAME
CALL WS-PGM-NAME USING WS-A.
o In Dynamic Call main program and Sub program will be having the
separate load modules.
o Programs to be called will be come to know at run time.
o The compiler option DYNAM has to be used.
o If there is a change in the sub-program then only sub-program has to
be recompiled.
Disadvantages:
o As the load modules are different to pick up the load module of sub
program into memory for execution takes a bit time.
TABLE HANDLING

In COBOL arrays handling is called as Table handling.


We can define the TABLE using the OCCURS clause in COBOL.
You cant define the OCCURS clause in the 01 level.
If one OCCCURS clause is there that means its ONE-DIMENSIONAL ARRAY.
If two OCCCURS clauses are there that means its TWO-DIMENSIONAL ARRAY
and so on.
You can define up to 7 OCCURS clauses in COBOL.

How to define the ONE-DIMENSTIONAL ARRAYS:20

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

01

STATE-TAB.
05 DIST-NAME PIC X(3) OCCURS 24 TIMES.

The above example defines 1-D table where it can store up to 24 rows where is
row size is 3 bytes.
Total Size - 3 * 24 ==> 72 bytes
How to define the TWO-DIMENSTIONAL ARRAYS:01

STUDENT-TAB.
05 NAME
10 SUBJECT

PIC X(20)
PIC 9(3)

OCCURS 30 TIMES.
OCCURS 6 TIMES.

In the above example there 30 students in the class and for each
Student there are 6 subjects.
How to define the ONE-DIMENSTIONAL ARRAYS DYNAMICALLY:01

STATE-TAB.
05 DIST-NAME PIC X(3) OCCURS 1 TO 100 TIMES DEPENDING ON WS-VAL.

If the WS-VAL contains 30 then it will defines up to 30 rows only.


How

to define the TABLE with INDEX option:INDEX is used to create the INDEX in the table.
Then INDEX is used for checking or retrieving the records in a faster manner.
We need to set the INDEX variable to 1.
To increment or decrement the value of the INDEX variable we need to use
SET UPBY or SET DOWNBY keywords.
We dont need to declare the index variables separately in the WORKINGSTORAGE SECTION.

01

STATE-TAB.
05 DIST-NAME PIC X(5) OCCURS 24 TIMES INDEX BY IDX.

Why we use INDEX?


INDEX is used when we want to do a SEARCH for a particular string in the table.
What is SEARCH and SEARCH ALL?
SEARCH 1. SEARCH is Sequential search.
2. SEARCH operates with the occurrence of an element.
21

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

3. In SEARCH the data need not in sorted order.


4. We can use any WHEN conditions in SEARCH.
5. We can use any relational operators in SEARCH.
SEARCH ALL1. SEARCH ALL is Binary search.
2. SEARCH operates with the displacement (Address) of an element.
3. In SEARCH the data needs to be in sorted order.
4. We can use only one WHEN condition in SEARCH.
5. We can use only equal operator in SEARCH.
6. This is the faster way searching an element.
7. If the table size is very huge then go for the SEARCH ALL.
EX:SET 1 TO IDX.
SEARCH DIST-NAME(IDX)
AT END
DISPLAY NAME NOT FOUND
WHEN DIST-NAME(IDX) = HYD
DISPLAY NAME FOUND
NOT AT END
SET IDX UPBY 1
END-SEARCH
INTERNAL SORT

If we do the SORTING in the COBOL program then its called the INTERNAL
sorting.
If we do the sorting in the JCL using the utility SORT then its called the
EXTERNAL sort.
For sorting we need to define the SORT work files in the JCL.
The SORT work files will be represented in the COBOL program using the SD
SD - SORT DESCRIPTION
For doing the SORT we required 3 files.
o Sort work file This file is required if the input file contains huge
records to sort.
o Input file
o Output file

Syntax:SORT work-file ON ASCENDING KEY key-name USING ip-file GIVING op-file.


22

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

FILE HANDLING

COBOL can handle PS files and VSAM files.

All the files used in the JCL needs to be declared in the ENVIRONMENT
DIVISION and also in the DATA DIVISION.

We can OPEN the file in the below modes.


1. INPUT
- To read the records from the file
2. OUTPUT
- To write the records into the file
3. I-O
- To read and write the records
4. EXTEND
-To append the records at the bottom of the file.

Follow the below steps in order to use the files in the COBOL program.
1. Declare the files in the INPUT-OUTPUT SECTION of ENVIRONMENT
DIVISION.
2. Declare the file attributes in the FILE SECTION of DATA DIVISION.
3. OPEN the file in one off the modes (INPUT, OUTPUT, I-O, EXTEND)
4. Use one off the statements (READ, WRITE, REWRITE, DELETE)
5. Close the file using the statement CLOSE.

PS FILE: In PS file, the records will be organized in Sequential order and accessed in
Sequential order only. So we need to mention ORGANIZATION and ACCESS
MODE as SEQUENTIAL.
After every operation on the file, OS will places the status code in the
variable declared using FILE STATUS.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IN-FILE ASSIGN TO INDD
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL
FILE STATUS IS WS-IN-FILE-STAT.
KSDS FILE: In KSDS file, the records will be organized based on the Index order and
accessed Sequentially or Dynamically or Randomly. Hence mention the
below values for KSDS.
ORGANIZATION IS INDEXED
ACCESS MODE IS SEQUENTIAL\RANDOM\DYNAMIC
And also you need to mention the Key as below.
23

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

RECORD KEY IS EMP-ID.

Ex-1
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IN-FILE ASSIGN TO KSDS-DD
ORGANIZATION IS INDEXED
ACCESS MODE IS SEQUENTIAL
FILE STATUS IS WS-IN-FILE-STAT.
Ex-2
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IN-FILE ASSIGN TO KSDS-DD
ORGANIZATION IS INDEXED
ACCESS MODE IS RANDOM
FILE STATUS IS WS-IN-FILE-STAT.
Ex-3
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IN-FILE ASSIGN TO KSDS-DD
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
FILE STATUS IS WS-IN-FILE-STAT.
ESDS FILE: In ESDS file, the records will be organized based on the Entry and accessed
sequentially. Hence mention the below values for ESDS.
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL
To differentiate the PS file and ESDS file you need to put AS- before the
DDNAME.
Ex-1
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IN-FILE ASSIGN TO AS-ESDS-DD
ORGANIZATION IS SEQUENTIAL
24

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

ACCESS MODE IS SEQUENTIAL


FILE STATUS IS WS-IN-FILE-STAT.
RRDS FILE: In RRDS file, the records will be organized based on the relative number and
accessed sequentially or randomly or dynamically. Hence mention the below
values for RRDS.
ORGANIZATION IS RELATIVE
ACCESS MODE IS SEQUENTIAL \ RANDOM \ DYNAMIC
Ex-1
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IN-FILE ASSIGN TO RRDS-DD
ORGANIZATION IS RELATIVE
ACCESS MODE IS SEQUENTIAL \ RANDOM \ DYNAMIC
FILE STATUS IS WS-IN-FILE-STAT.
Syntax to OPEN the file: OPEN
OPEN
OPEN
OPEN

INPUT
OUTPUT
I-O
EXTEND

IN-FILE
OUT-FILE
IN-FILE
IN-FILE

IF WS-IN-FILE-STAT = 00
CONTINUE
ELSE
DISPLAY ERROR IN OPENING THE FILE
END-IF.

Syntax to READ from the file: READ IN-FILE INTO WS-IN-REC


AT END
MOVE YES
END-READ.

TO

EOF

Syntax to WRITE into the file: 25

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

WRITE OUT-FILE-REC FROM IN-FILE-REC.


Example program to read from the PS file and write into the PS file.
IDENTIFICATION DIVISION.
PROGRAM-ID. PSFILE.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IN-FILE ASSIGN TO IN-DD
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL
FILE STATUS IS WS-STAT1.
SELECT OUT-FILE ASSIGN TO OUT-DD
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL
FILE STATUS IS WS-STAT2.
DATA DIVISION.
FILE SECTION.
FD IN-FILE.
LABEL RECORDS ARE STANDARD
RECORD CONTAINS 80 CHARACTERS
BLOCK CONTAINS 0 RECORDS.
01 IN-REC
PIC X(80).
FD OUT-FILE.
LABEL RECORDS ARE STANDARD
RECORD CONTAINS 80 CHARACTERS
BLOCK CONTAINS 0 RECORDS.
01 OUT-REC
PIC X(80).
WORKING-STORAGE SECTION.
01 WS-IN-REC.
05 WS-IN-EID
PIC 9(5).
05 WS-IN-ENAME
PIC X(30).
05 WS-IN-ADDR
PIC X(30).
05 WS-IN-SALARY
PIC 9(5).
05 WS-IN-DEPTNAME
PIC X(10).
01 WS-OUT-REC.
05 WS-OUT-EID
PIC 9(5).
05 WS-OUT-ENAME
PIC X(30).
05 WS-OUT-ADDR
PIC X(30).
05 WS-OUT-SALARY
PIC 9(5).
05 WS-OUT-DEPTNAME
PIC X(10).
77 EOF-IN
PIC X(3).
77 WS-STAT-1
PIC X(2).
77 WS-STAT-2
PIC X(2).
PROCEDURE DIVISION.
100-INITIAL-PARA.
DISPLAY ' PROGRAM STARTED '.
PERFORM 200-OPEN-FILES.
PERFORM 300-READ-INFILE UNTIL EOF-IN = 'YES'.
26

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,
PERFORM 400-CLOSE-FILES.
DISPLAY ' PROGRAM ENDED '.
STOP RUN.
100-EXIT.
EXIT.
200-OPEN-FILES.
OPEN INPUT IN-FILE
OUTPUT OUT-FILE.
IF WS-STAT1 = '00'
CONTINUE
ELSE
DISPLAY ' ERROR IN OPENING INPUT FILE '
STOP RUN
END-IF.
IF WS-STAT2 = '00'
CONTINUE
ELSE
DISPLAY ' ERROR IN OPENING OUTPUT FILE '
STOP RUN
END-IF.
200-EXIT.
EXIT.
300-READ-INFILE.
READ INFILE INTO WS-IN-REC
AT END
MOVE 'YES'
END-READ.
MOVE
MOVE
MOVE
MOVE
MOVE

WS-IN-EID
WS-IN-ENAME
WS-IN-ADDR
WS-IN-SALARY
WS-IN-DEPTNAME

WRITE OUT-REC

TO EOF-IN
TO
TO
TO
TO
TO

WS-OUT-EID
WS-OUT-ENAME
WS-OUT-ADDR
WS-OUT-SALARY
WS-OUT-DEPTNAME

FROM WS-OUT-REC.

300-EXIT.
EXIT.
400-CLOSE-FILES.
CLOSE IN-FILE, OUT-FILE.
400-EXIT.
EXIT.

27

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

DB2 - DATABASE 2

DB2 is called as Universal Database as it can be accommodated with any


operating system or any programming language.
DB2 is a RDBMS (Relational Database Management System) which contains
Tables. And Table contains data in Rows and Columns.
Rows are called as Records and Columns are called as Fields.

DB2 SQL commands are categorized into 3 types.


1. DDL - DATA DEFINITION LANGUAGE
2. DML - DATA MANIPULATION LANGUAGE
3. DCL - DATA CONTROL LANGUAGE
DATA DEFINITON LANGUAGE - These statements are used to create, drop, and
alter the tables.
CREATE
ALTER
DROP
DECLARE
CREATE: - This statement is used to create the following db2 objects.
Storage Groups
Databases
Table spaces
Tables
28

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

Indexes
Triggers
Views
Stored Procedures

Storage Group: A Storage Group is a group of volumes on DASD (direct access storage
device). The Volumes hold the data sets in which tables and indexes are
actually stored.

The DB2 catalog table SYSIBM.SYSSTOGROUP contains a row for each


storage group that you define, and the table SYSIBM.SYSVOLUMES contains a
row for each DASD volume. At installation, the system default storage group
is defined. This storage group is named SYSDEFLT. If you do not explicitly
manage your storage, then DB2 uses the default storage group to allocate
space.

Data base: A data base is stored in the Storage group


A data base contains the table spaces.
Table Spaces: A table space contains the tables and is to be stored in the data base.
In this the space is divided into pages and each page size is 4 KB, 8KB, 16KB,
32KB. Default is 4KB.
There are 3 types of Table spaces.
o Simple Table space
o Segmented Table space
o Partitioned Table space
Simple Table space - Here the table space contain more than one tables.
Segmented Table space - Here the space is divided into equal parts called as
segments and each segment contains rows from only table.
Partitioned Table space - Here the space is divided into Partitions and each
partition is assigned to only one table.

29

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

How to create the Table space:CREATE TABLESPACE MYTS


IN MYDB
USING STOGROUP MYSTOGRP
PRIQTY 30720
SECQTY 10240
SEGSIZE 32
==> SEGEMENTED TABLE SPACE
LOCKSIZE TABLE
BUFFERPOOL BP0
CLOSE NO;
CREATE TABLESPACE SALESHX
IN MYDB
USING STOGROUP MYSTOGRP
PRIQTY 4000
SECQTY 130
ERASE NO
DSSIZE 16G
NUMPARTS 48
(PARTITION 46
==> PARTITIONED TABLE SPACE
COMPRESS YES,
PARTITION 47
COMPRESS YES,
PARTITION 48
COMPRESS YES)
LOCKSIZE PAGE
BUFFERPOOL BP1
CLOSE NO;
Table A table can contain the data in the form of rows and columns.
How to create the Table:

In IBM-DB2 the table has to be stored in the Table space, where the table
space is stored in the Database.

CREATE TABLE EMPLOYEE


(
EMP_ID
SMALLINT NOT NULL PRIMARY KEY,
EMP_FIRST_NAME
CHAR(30),
EMP_LAST_NAME
CHAR(25),
SALARY
DECIMAL(5,2),
DEPT
CHAR(10)
) USING FSSADMDB.FSSADMTS
30

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

How to define the Primary Key separately


CREATE UNIQUE INDEX FSS106DB.EMPXDX
ON FSS106DB.EMPLOYEE
(EMP_ID ASC)
USING STOGROUP DSN8G910
PRIQTY 512
SECQTY 64
ERASE NO
BUFFERPOOL BP1
CLOSE YES
DB2 keys:Unique Keys A unique key contains the unique values
You can have any number of Unique keys in a table
A Unique key cant contain null values
Primary Keys A Primary key is a special Unique key
It also does not contain any unique values
And you should have only one Primary key for a table.
Foreign Key A Foreign key enforces a relation between two tables.
In the below figure we have two tables DEPT, EMPLOYEE.
DEPT table - Here DEPTNO is the PK and MGRNO is the FK.
EMPLOYEE table - Here EMPNO is the PK and DEPTNO is the FK
PK
DEPTN
O
C01
D11
E21

FK
DEPTNAM MGRN
E
O
PAYROLL
30
FINANCE
60
ADMIN
-----

PK
EMPNO

LASTNAM
E

FK
DEPTN
O

ROLE
31

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

30 KUMAR
200 BROWN
340 JOHN

C01
D11
E21

MGR
SE
SSE

ALTER - this command is used to alter the table, table space and other objects.
ALTER TABLE EMPLOYEE
DROP - This command is used to drop the table. When we drop the table all the
rows and columns of it will be automatically deleted.
DROP TABLE EMPLOYEE
DROP INDEX EMPIDX
DATA CONTROL LANGUAGE - This is used for controlling the access on the db2
objects.
GRANT, REVOKE are the commands used in this language.
GRANT - To give the access to a particular user id or to all the users on the db2
objects we use this command.
GRANT ON TABLE EMPLOYEE ALL TO PUBLIC This will give all the accesses to all
the users.
GRANT ON TABLE EMPLOYEE SELECT, INSERT TO FSS117 This will give the
SEELCT, INSERT access only to the user id FSS117.
REVOKE - Is used to revoke the access.
DATA MANIPULATION LANGUAGE - This is used for controlling the data stored
on the database.
1.
2.
3.
4.

SELECT
INSERT
UPDATE
DELETE

32

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

SELECT - SELECT statement is used to get all the data or required data from the
tables.
Types of SELECT statements:SELECT * FROM EMPLOYEE This will return all rows & columns from the
EMPLOYEE table
2. SELECT EMPID, EMPNAME FROM EMPLOYEE This will return only the
columns EMPID, EMPNAME from EMPLOYEE table.
3. SELECT DISTINCT(DEPTNAME) FROM DEPT This will eliminate the duplicate
rows from DEPTNAME column and display the result.
1.

WHERE clause - The WHERE clause is used for checking the condition before
retrieving the results from the table.
SELECT EMPID, EMPNAME
FROM EMPLOYEE
WHERE EMPID = 1001;
1001

This will give only one row where EMPID -

You can also use any logical operator (AND, OR, NOT)
SELECT EMPID, EMPNAME
FROM EMPLOYEE
WHERE EMPID = 1001 AND EMPID = 1002;
EMPID - 1001 & 1002.

This will give rows where

BETWEEN Operator - This will check the range of values and display only the
matched rows.
SELECT EMPID, EMPNAME, SALARY
FROM EMPLOYEE
WHERE EMPID BETWEEN 1001 AND 1100 This will retrieve all the rows where
EMPID is in between 1001 to 1100.
IN Operator - This will check the list of values and display only the matched
rows.
SELECT EMPID, EMPNAME, SALARY
FROM EMPLOYEE
33

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

WHERE EMPID IN (1001,1003,1010,1100) This will retrieve all the rows where
EMPID is in the list 1001, 1003, 1010, 1100.
LIKE Operator - LIKE operator is used with the character data types and is used
to check for the wild card strings.
If we want find the rows where the EMPNAME starts with KUMAR.
SELECT EMPID, EMPNAME, SALARY
FROM EMPLOYEE
WHERE EMPNAME LIKE KUMAR% This will retrieve all the rows where
EMPNAME starts with the string KUMAR

% Wild card character which checks for the entire string.


_ To check for a particular character.

Ex1 - Retrieve all the rows where 3rd character of the EMPNAME is M.
SELECT EMPID, EMPNAME, SALARY
FROM EMPLOYEE
WHERE EMPNAME LIKE __M% Here first two characters are (_) and then
written the 3rd character as M and then %.
DB2 Column functions Below are the most common used DB2 column functions.
1. COUNT
2. AVG
3. MAX
4. MIN
5. SUM
COUNT - This function is used to count the number of rows in a db2 table.
SELECT COUNT(*) FROM EMPLOYEE This will return the number of rows
(Including duplicate)
SELCT COUNT(DISTINCT *) FROM EMPLOYEE This will return the number of rows
(Excluding duplicate)
AVG - This function is used to find the average value of a column.
SELECT AVG(SALARY) FROM EMPLOYEE This will return the average salary of
each employee
34

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

MAX - This will return the maximum value of a column.


SELECT MAX(SALARY) FROM EMPLOYEE This will give the maximum salary from
the EMPLOYEE table.
MIN - This will return the minimum value of a column.
SELECT MIN(SALARY) FROM EMPLOYEE This will give the minimum salary from
the EMPLOYEE table.
SUM - This will add the values of a column and produce the sum.
SELECT SUM(SALARY) FROM EMPLOYEE This will produce the sum of all the
employees salary.
GROUP BY - This clause is used to group the result records into one row and
display the result.
Ex - To find the average salary of each department from the EMPLOYEE table.
SELECT DEPTNAME, AVG(SALARY)
FROM EMPLOYEE
GROUP BY DEPTNAME
ORDER BY - This clause is used to display the results in Ascending or Descending
order.
SELECT DEPTNAME, AVG(SALARY)
FROM EMPLOYEE
GROUP BY DEPTNAME
ORDER BY DEPTNAME DESC;
HAVING - HAVING clause is used to check the condition on the result rows and then
display only the matching rows.
Difference between WHERE and HAVING?

The WHERE clause selects rows before grouping and HAVING clause selects
rows after grouping.
The WHERE clause can be used without GROUP BY clause and HAVING has to
be used with the GROUP BY clause.

INSERT - INSERT statement is used to insert the rows into the DB2 table.
35

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

There are three forms of this statement:


The INSERT using VALUES form is used to insert a single row into the
table or view using the values provided or referenced.

The INSERT using SELECT form is used to insert one or more rows into
the table or view using values from other tables or views.

The INSERT using n ROWS form is used to insert multiple rows into the
table or view using the values provided in a host-structure-array.

INSERT INTO DEPARTMENT


VALUES ('E31', 'ARCHITECTURE', '00390', 'E01')
INSERT INTO MA_EMPPROJACT
SELECT * FROM EMPPROJACT
INSERT INTO DEPARTMENT 10 ROWS VALUES (:DEPT)
INSERT INTO EMPPROJACT
VALUES ('000140', 'PL2100', 30)
WITH CHG

If a row is inserted into the DB2 table you will get the SQLCODE = 0 to indicate it
has inserted successfully.
If SQLCODE = -803 then it means a duplicate record exist.

UPDATE - UPDATE statement is used to update the columns of the DB2 table.
UPDATE EMPLOYEE
SET SALARY = 10000
WHERE EMPID = 1001; This will set the salary to 10000 for the EMPID 1001
UPDATE EMPLOYEE
SET SALARY = SALARY + 1000 This will increment the salary of all the
employees to 10000.
DELETE - DELETE statement is used to delete the rows of the DB2 table.
DELETE FROM EMPLOYEE WHERE EMPID = 1001 Deletes only one row.
DELETE FROM EMPLOYEE Deletes all the rows from Employee table.
COMPILATION PROCESS
The compilation process for COBOL-DB2 program includes various steps.
1. PRE COMPILATION
36

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

2.
3.
4.
5.

COMPILATION
LINK EDIT
BIND
EXECUTE

PRE COMPILATION In this process the compiler checks for the all the SQL statements which are
embedded within the EXEC SQL and END-EXEC statements and puts into a
module called as DBRM.
And converts all the SQL statements into COBOL CALL statements.
And it places a TIME STAMP on the DBRM module.
COMPILATION It will do a normal Compilation
BIND - BIND reads the sql statements from DBRM and produces the access path
logic to retrieve the data from DB2.
It contains two types.
1. PACKAGE
2. PLAN.
PACKAGE A PACKAGE takes DBRM as input and produces a single package containing
the optimized access path logic.
The Package then can be bound into Plan using BIND PLAN command.
A Package cant be executable so we need to have the PLAN to execute a
Cobol-DB2 program.
The advantage of the PACKAGE is if one of the program is changed instead of
binding the entire plan will just BIND the impacted program. So it will save
the binding time and cost.
PLAN A PLAN take one or more DBRMs or one or more PACKAGEs as input and
produces the application plan.
A PLAN is required to execute the COBOL-DB2 program.
Parameters used in the BIND card BIND PLAN(FSS106) MEMBER(CRC210A0) ACTION(REP) ACQUIRE(USE) RELEASE(COMMIT) 37

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

ISOLATION(CS) OWNER(FSS106)

PLAN name - Here in Training we give the plan name as the user id.
MEMBER - Your DBRM name has to give here. Nothing but your program name.
ACTION - You can use two options here
ADD - To add the member name into the PLAN list.
REP - To replace the already existing member name in the PLAN list.
ACQUIRE and RELEASE These two options of BIND will determine when to lock the db2 object (Table,
Partition or Tablespace) and when to release the lock on the db2 object.
ACQUIRE(ALLOCATE) - Acquires the lock when the DB2 object is allocated. This
option is not allowed when its BIND or REBIND.
ACQUIRED(USE) - Acquires the lock when the object is first accessed.
RELEASE(DEALLOCATE) - Releases the lock when the object is deallocated i.e.
when your program execution is completed.
RELEASE(COMMINT) - Releases the lock when there is an explicit COMMIT
statement in your program.
ISOLATION LEVELS The ISOLATION option on the BIND card determines the types of locking the table.
There are four types of ISOLATION levels.
1.
2.
3.
4.

CURSOR STABILITY
READ STABILITY
REPEATABLE READ
UNCOMMITED READ

1. CURSOR STABILITY - In this DB2 locks the table on the page wise and this is the
default lock. Once the page scan is done then it will release the lock and then
move to the next page.
2. READ STABILITY - In this DB2 locks each row on the table and does not allow any
process until the lock is released.
3. REPEATABLE READ - In this DB2 locks all the rows of the table and does not
allow any INSERT, DELETE, UPDATE operations from other processes until the first
process is end.
38

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

4. UNCOMMITED READ - In this DB2 locks all the rows of the table and does not
allow any other processes until an explicit commit is done.

INCLUDE and COPY statements


INCLUDE and COPY statements are used to include the copybooks in the COBOL
program.
INCLUDE statement expands the copybooks in the pre-compilation stage. So all
your SQL copybooks should be mentioned using the INCLUDE statement.
Ex -

EXEC SQL
INCLUDE SQLCA
END-EXEC.

COPY statement expands the copybooks at the time of compilation stage, so if


there are db2 errors it will not be caught at the time of pre-compilation stage.

JOINS
Sometimes you may required to retrieve some columns from TABLE-1 and some
columns of TABLE-2. In this case you will go for the JOIN to combine two tables and
retrieve the required columns.

DB2 supports below joins.


1. INNER JOIN OR SIMPLE JOIN
2. LEFT OUTER JOIN
3. RIGHT OUTER JOIN
4. FULL OUTER JOIN Or OUTER JOIN

You need to write the JOIN in the FROM clause of SELECT statement.

EMPLOYEE TABLE ENO


1001
1002
1003
1004
1005
1006
1007

NAME
PHANI
KUMAR
INDRA
REDDY
MAHESH
RAGHU
RAGHAV

SALARY
30000
45000
22000
48000
12300
26300
87564

DEPTNO
D001
D001
D002
D002
D001
D003
D003
39

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,
1008 RAVI
15600
1009 MANOJ
98500
1010 SIMHA
35000
1011 KISHORE
43000

D002
D003
D001
----

DEPT TABLE DEPTNO


D001
D002
D003
D004

DEPTNAME
SOFTWARE
PAYROLL
FINANCE
TRANSPORT

INNER JOIN In this it will give only the rows that match the condition.
SELECT A.ENO, A.NAME, B.DEPTNAE
FROM EMPLOYEE A, DEPT B
WHERE A.DEPTNO = B.DEPTNO
OR
SELECT A.ENO, A.NAME, B.DEPTNAE
FROM EMPLOYEE A INNER JOIN DEPT B
WHERE A.DEPTNO = B.DEPTNO
O\P
ENO
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010

NAME
PHANI
KUMAR
INDRA
REDDY
MAHESH
RAGHU
RAGHAV
RAVI
MANOJ
SIMHA

DEPTNAME
SOFTWARE
SOFTWARE
PAYROLL
PAYROLL
SOFTWARE
FINANCE
FINANCE
PAYROLL
FINANCE
SOFTWARE

NOTE - It has display only the 10 rows of EMPLOYEE table and has not
displayed ENO - 1011 as there is no department like ---- in DEPT
table.
LEFT OUTER JOIN 40

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

In this it will give only the rows that match the condition and also
the non matching rows from the left side of the table.
SELECT A.ENO, A.NAME, B.DEPTNAE
FROM EMPLOYEE A LEFT OUTER JOIN DEPT B
WHERE A.DEPTNO = B.DEPTNO
ENO
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011

NAME
PHANI
KUMAR
INDRA
REDDY
MAHESH
RAGHU
RAGHAV
RAVI
MANOJ
SIMHA
KISHORE

DEPTNAME
SOFTWARE
SOFTWARE
PAYROLL
PAYROLL
SOFTWARE
FINANCE
FINANCE
PAYROLL
FINANCE
SOFTWARE
----

1011 - Is a non matching row but still we got it in the output and the reason for this
is LEFT OUTER JOIN returns the non matching rows also from the left side of the
table.
RIGHT OUTER JOIN In this it will give only the rows that match the condition and also
the non matching rows from the right side of the table.
SELECT A.ENO, A.NAME, B.DEPTNAE
FROM EMPLOYEE A RIGHT OUTER JOIN DEPT B
WHERE A.DEPTNO = B.DEPTNO
ENO
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
----

NAME
PHANI
KUMAR
INDRA
REDDY
MAHESH
RAGHU
RAGHAV
RAVI
MANOJ
SIMHA
----

DEPTNAME
SOFTWARE
SOFTWARE
PAYROLL
PAYROLL
SOFTWARE
FINANCE
FINANCE
PAYROLL
FINANCE
SOFTWARE
TRANSPORT
41

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

FULL OUTER JOIN In this it will give only the rows that match the condition and also
the non matching rows from the left side of the table and then right
side of the table.
SELECT A.ENO, A.NAME, B.DEPTNAE
FROM EMPLOYEE A FULL OUTER JOIN DEPT B
WHERE A.DEPTNO = B.DEPTNO
ENO
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
----

NAME
PHANI
KUMAR
INDRA
REDDY
MAHESH
RAGHU
RAGHAV
RAVI
MANOJ
SIMHA
KISHORE
----

DEPTNAME
SOFTWARE
SOFTWARE
PAYROLL
PAYROLL
SOFTWARE
FINANCE
FINANCE
PAYROLL
FINANCE
SOFTWARE
---TRANSPORT

42

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

UNION & UNION ALL This is used to merge the rows of same table or different tables.
It works based on the rows.
When you use the UNION the column names in both queries should have
same name and should contain same number of columns.
UNION will eliminate the duplicate rows.
SELECT EMPNO
FROM DSN8810.EMP
WHERE WORKDEPT = 'D11'
UNION ALL
SELECT EMPNO
FROM DSN8810.EMPPROJACT
WHERE PROJNO = 'MA2112' OR
PROJNO = 'MA2113' OR
PROJNO = 'AD3111'
ORDER BY EMPNO;
COMMIT & ROLLBACK options COMMIT - This option is used to commit the work done. That means db2 saves the
data what ever is inserted, updated, deleted till that point of time.
EXEC SQL
COMMIT
END-EXEC.
ROLLBACK - This option is used to not save the work done. Opposite of COMMIT.
EXEC SQL
ROLLBACK
END-EXEC.

43

MAINFRAME MATERIAL - COBOL, DB2, AND CICS


AUTHOR - K PHANI KUMAR,

44

You might also like