Beruflich Dokumente
Kultur Dokumente
Contents
Contents...................................................................................................................................................................1
DIVISIONS........................................................................................................................................................3
DATA TYPES....................................................................................................................................................6
IF STATEMENT ..............................................................................................................................................13
SUB PROGRAMS...........................................................................................................................................18
TABLE HANDLING..........................................................................................................................................20
INTERNAL SORT............................................................................................................................................22
FILE HANDLING.............................................................................................................................................23
2
MAINFRAME MATERIAL - COBOL, DB2, AND CICS
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:-
• It’s 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:-
• It’s 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
• 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:-
• It’s 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.
• LINKAGE SECTION - It’s 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) VALUE 0 COMP-3.
77 RECORDS-SELECTED PIC 9(7) VALUE 0 COMP-3.
LINKAGE SECTION.
PROCEDURE DIVISION:-
• It’s 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.
4
MAINFRAME MATERIAL - COBOL, DB2, AND CICS
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.
5
MAINFRAME MATERIAL - COBOL, DB2, AND CICS
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.
Ex-
6
MAINFRAME MATERIAL - COBOL, DB2, AND CICS
01 WS-NAME.
05 INITIAL PIC X(1) VALUE SPACES.
05 SUR-NAME PIC X(1) VALUE SPACES.
05 FIRST-NAME PIC X(1) VALUE SPACES.
PICTURE Clause: 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.
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.
01 WS-NAME.
05 INITIAL PIC X(1) VALUE SPACES.
05 SUR-NAME PIC X(15) VALUE SPACES.
05 FIRST-NAME PIC X(20) VALUE SPACES.
7
MAINFRAME MATERIAL - COBOL, DB2, AND CICS
01 WS-INPUT.
05 RECORD-1.
10 FIELD-A PIC 9(2) VALUE 10.
10 FIELD-B PIC 9(2) VALUE 20.
05 RECORD-2.
10 FIELD-C PIC 9(2) VALUE 30.
10 FIELD-D PIC 9(2) VALUE 40.
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
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
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 X(15).
05 AGE PIC 9(3).
05 SALARY PIC 9(5)V9(2).
01 EMP-REC-2.
05 NAME PIC X(15).
05 AGE PIC 9(3).
05 SALARY PIC 9(5)V9(2).
05 ADDR PIC X(30).
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
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).
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
O\P - 25
10
2
5
COMPUTE VERB
• For doing large arithmetic operations we can’t 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.
Ex-
COMPUTE 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:-
12
MAINFRAME MATERIAL - COBOL, DB2, AND CICS
IF STATEMENT
• IF statement is used for decision making in COBOL.
Format-1:-
IF <CONDITION> THEN
Statement1
Statement2
Statement3
END-IF.
Statement-X.
Format-2:-
IF <CONDITION> THEN
Statement1
Statement2
Statement3
ELSE
Statement4
Statement5
Statement6
END-IF.
Ex-
IF AMT > 0
DISPLAY ‘AMOUNT IS POSITIVE ‘ AMT
ELSE
DISPLAY ‘AMOUNT IS NEGATIVE ‘ AMT
END-IF.
13
MAINFRAME MATERIAL - COBOL, DB2, AND CICS
Format-1:-
Ex-
EVALUATE 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:-
Ex-
EVALUATE 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
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.
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.
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
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
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.
PROCEDURE DIVISION.
100-MAIN-PARA.
DISPLAY ‘HELLOW IBM MAINFRAMES‘.
EXIT PROGRAM.
100-EXIT.
EXIT.
17
MAINFRAME MATERIAL - COBOL, DB2, AND CICS
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
LINKAGE SECTION.
18
MAINFRAME MATERIAL - COBOL, DB2, AND CICS
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
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.
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 can’t 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.
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.
01 STUDENT-TAB.
05 NAME PIC X(20) OCCURS 30 TIMES.
10 SUBJECT PIC 9(3) OCCURS 6 TIMES.
In the above example there 30 students in the class and for each
Student there are 6 subjects.
01 STATE-TAB.
05 DIST-NAME PIC X(3) OCCURS 1 TO 100 TIMES DEPENDING ON WS-VAL.
01 STATE-TAB.
05 DIST-NAME PIC X(5) OCCURS 24 TIMES INDEX BY IDX.
SEARCH ALL-
1. 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 it’s called the INTERNAL
sorting.
• If we do the sorting in the JCL using the utility SORT then it’s 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:-
22
MAINFRAME MATERIAL - COBOL, DB2, AND CICS
• All the files used in the JCL needs to be declared in the ENVIRONMENT
DIVISION and also in the DATA DIVISION.
• Follow the below steps in order to use the files in the COBOL program.
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
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
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.
IF WS-IN-FILE-STAT = ‘00’
CONTINUE
ELSE
DISPLAY ‘ERROR IN OPENING THE FILE’
END-IF.
25
MAINFRAME MATERIAL - COBOL, DB2, AND CICS
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.
26
MAINFRAME MATERIAL - COBOL, DB2, AND CICS
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' TO EOF-IN
END-READ.
300-EXIT.
EXIT.
400-CLOSE-FILES.
CLOSE IN-FILE, OUT-FILE.
400-EXIT.
EXIT.
27
MAINFRAME MATERIAL - COBOL, DB2, AND CICS
DB2 - DATABASE 2
DATA DEFINITON LANGUAGE - These statements are used to create, drop, and
alter the tables.
CREATE
ALTER
DROP
DECLARE
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.
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
Table -
• A table can contain the data in the form of rows and columns.
• In IBM-DB2 the table has to be stored in the Table space, where the table
space is stored in the Database.
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 can’t 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.
PK FK
DEPTN DEPTNAM MGRN
O E O
C01 PAYROLL 30
D11 FINANCE 60
E21 ADMIN -----
PK FK
LASTNAM DEPTN
EMPNO E O ROLE
31
MAINFRAME MATERIAL - COBOL, DB2, AND CICS
ALTER - this command is used to alter the table, table space and other objects.
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.
DATA CONTROL LANGUAGE - This is used for controlling the access on the db2
objects.
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.
DATA MANIPULATION LANGUAGE - This is used for controlling the data stored
on the database.
1. SELECT
2. INSERT
3. UPDATE
4. DELETE
32
MAINFRAME MATERIAL - COBOL, DB2, AND CICS
SELECT - SELECT statement is used to get all the data or required data from the
tables.
1. 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.
WHERE clause - The WHERE clause is used for checking the condition before
retrieving the results from the table.
You can also use any logical operator (AND, OR, NOT)
BETWEEN Operator - This will check the range of values and display only the
matched rows.
IN Operator - This will check the list of values and display only the matched
rows.
33
MAINFRAME MATERIAL - COBOL, DB2, AND CICS
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.
Ex1 - Retrieve all the rows where 3rd character of the EMPNAME is M.
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)
SELECT AVG(SALARY) FROM EMPLOYEE This will return the average salary of
each employee
34
MAINFRAME MATERIAL - COBOL, DB2, AND CICS
SELECT MAX(SALARY) FROM EMPLOYEE This will give the maximum salary from
the EMPLOYEE table.
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
employee’s 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.
HAVING - HAVING clause is used to check the condition on the result rows and then
display only the matching rows.
• 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
• 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.
If a row is inserted into the DB2 table you will get the SQLCODE = 0 to indicate it
has inserted successfully.
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
employee’s 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
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.
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 can’t 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.
BIND PLAN(FSS106) -
MEMBER(CRC210A0) -
ACTION(REP) -
ACQUIRE(USE) -
RELEASE(COMMIT) -
37
MAINFRAME MATERIAL - COBOL, DB2, AND CICS
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.
ACQUIRE(ALLOCATE) - Acquires the lock when the DB2 object is allocated. This
option is not allowed when it’s BIND or REBIND.
ISOLATION LEVELS -
The ISOLATION option on the BIND card determines the types of locking the table.
There are four types of ISOLATION levels.
1. CURSOR STABILITY
2. READ STABILITY
3. REPEATABLE READ
4. 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
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.
Ex - EXEC SQL
INCLUDE SQLCA
END-EXEC.
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.
• You need to write the JOIN in the FROM clause of SELECT statement.
EMPLOYEE TABLE -
ENO NAME SALARY DEPTNO
1001 PHANI 30000 D001
1002 KUMAR 45000 D001
1003 INDRA 22000 D002
1004 REDDY 48000 D002
1005 MAHESH 12300 D001
1006 RAGHU 26300 D003
1007 RAGHAV 87564 D003
39
MAINFRAME MATERIAL - COBOL, DB2, AND CICS
DEPT TABLE -
DEPTNO DEPTNAME
D001 SOFTWARE
D002 PAYROLL
D003 FINANCE
D004 TRANSPORT
INNER JOIN -
In this it will give only the rows that match the condition.
OR
O\P
ENO NAME DEPTNAME
1001 PHANI SOFTWARE
1002 KUMAR SOFTWARE
1003 INDRA PAYROLL
1004 REDDY PAYROLL
1005 MAHESH SOFTWARE
1006 RAGHU FINANCE
1007 RAGHAV FINANCE
1008 RAVI PAYROLL
1009 MANOJ FINANCE
1010 SIMHA 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.
40
MAINFRAME MATERIAL - COBOL, DB2, AND CICS
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.
41
MAINFRAME MATERIAL - COBOL, DB2, AND CICS
42
MAINFRAME MATERIAL - COBOL, DB2, AND CICS
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 - 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
44