COBOL

0
54

COBOL – Overlook at

Introduction to COBOL

COBOL is a high-level language. One must belowstand the way COBOL works. Complaceers only belowstand machine code, a binary stream of 0s and 1s. COBOL code must become converted into machine code uperform a compiler. Run the program source through a compiler. The compiler 1st checks for any kind of kind of syntax errors and then converts it into machine language. The compiler generates a out generally thereplace file which is known as load module. This out generally thereplace file contains executable code in the form of 0s and 1s.

Evolution of COBOL

During 1950s, when the businesses were glineing in the western part of the world, generally generally there was a need to automate various processes for relayve of operation and this particular provided birth to a high-level programming language meant for business data procesperform.

  • In 1959, COBOL was makeed simply simply by CODASYL (Conference on Data Systems Language).

  • The next version, COBOL-61, was relrelayved in 1961 with a couple of revisions.

  • In 1968, COBOL was approved simply simply by ANSI as a standard language for commercial use (COBOL-68).

  • It was again revised in 1974 and 1985 to make subsequent versions named COBOL-74 and COBOL-85 respectively.

  • In 2002, Object-Oriented COBOL was relrelayved, which could use encapsulated objects as a normal part of COBOL programming.

Imslotance of COBOL

  • COBOL was the 1st widely used high-level programming language. It is an English-like language which is user friendly. All the instructions can become coded in easy English words.

  • COBOL is also used as a self-documenting language.

  • COBOL can handle huge data procesperform.

  • COBOL is compatible with it’s previous versions.

  • COBOL has effective error messages and so, resolution of bugs is easier.

Features of COBOL

Standard Language

COBOL is a standard language thead wear can become compiimmediateed and executed on machines such as IBM AS/400, private complaceers, etc.

Business Oriented

COBOL was styleed for business-oriented applications related to financial domain, defense domain, etc. It can handle huge volumes of data becomecause of it’s advanced file handling capabilitie ups.

Robust Language

COBOL is a robust language as it’s many kind of debugging and checcalifornia king tools are available for althe majority of all complaceer platforms.

Structucrimson Language

Logical manage structures are available in COBOL which generates it easier to read and modify. COBOL has different divisions, so it is easy to debug.

COBOL – Environment Setup

Installing COBOL on Windows/Linux

There are many kind of kind of Free Mainframe Emulators available for Windows which can become used to write and learn easy COBOL programs.

One such emulator is Hercules, which can become easily instalimmediateed on Windows simply simply by folloearng a couple of easy steps as given becomelow:

  • Download and install the Hercules emulator, which is available from the Hercules' home sit downe: www.hercules-390.eu

  • Once you have instalimmediateed the package on Windows machine, it will generate a folder like C:/hercules/mvs/cobol.

  • Run the Command Prompt (CMD) and revery the immediateory C:/hercules/mvs/cobol on CMD.

  • The comppermite guide on various commands to write and execute a JCL and COBOL programs can become found at:

  • www.jaymoseley.com/hercules/installmvs/instmvs2.htm

Hercules is an open up-source smoothbattlee implementation of the mainframe System/370 and ESA/390 architectures, in addition to the lacheck 64-bit z/Architecture. Hercules operates below Linux, Windows, Solaris, FreeBSD, and Mac OS X.

A user can connect to a mainframe server in a numbecomer of ways such a thin csitnt, dummy terminal, Virtual Csitnt System (VCS), or Virtual Desktop System (VDS). Every valid user is given a login id to enter into the Z/OS interface (TSO/E or ISPF).

Compiling COBOL Programs

In order to execute a COBOL program in batch mode uperform JCL, the program needs to become compiimmediateed, and a load module is generated with all the sub-programs. The JCL uses the load module and not the actual program at the time of execution. The load libraries are concatenated and given to the JCL at the time of execution uperform JCLLIB or STEPLIB.

There are many kind of kind of mainframe compiler utilitie ups available to compile a COBOL program. Some corporate companies use Change Management tools like Endevor, which compiles and stores every version of the program. This is helpful in tracruler the modifys made to the program.

//COMPILE   JOB ,CLASS=6,MSGCLASS=X,NOTIFY=&SYSUID             
//*            
//STEP1     EXEC IGYCRCTL,PARM=RMODE,DYNAM,SSRANGE
//SYSIN     DD DSN=MYDATA.URMI.SOURCES(MYCOBB),DISP=SHR
//SYSLIB    DD DSN=MYDATA.URMI.COPYBOOK(MYCOPY),DISP=SHR
//SYSLMOD   DD DSN=MYDATA.URMI.LOAD(MYCOBB),DISP=SHR
//SYSPRINT  DD SYSOUT=*
//*

IGYCRCTL is an IBM COBOL compiler utility. The compiler options are compenableeed uperform the PARM parameter. In the above example, RMODE instructs the compiler to use relative addresperform mode in the program. The COBOL program is compenableeed uperform the SYSIN parameter. Copybook is the library used simply simply by the program in SYSLIB.

Executing COBOL Programs

Give becomelow is a JCL example where the program MYPROG is executed uperform the inplace file MYDATA.URMI.INPUT and produces 2 out generally thereplace files composed to the spool.

//COBBSTEP  JOB CLASS=6,NOTIFY=&SYSUID
//
//STEP10    EXEC PGM=MYPROG,PARM=ACCT5000
//STEPLIB   DD DSN=MYDATA.URMI.LOADLIB,DISP=SHR
//INPUT1    DD DSN=MYDATA.URMI.INPUT,DISP=SHR
//OUT1      DD SYSOUT=*
//OUT2      DD SYSOUT=*
//SYSIN     DD *
//CUST1     1000
//CUST2     1001
/*

The load module of MYPROG is located in MYDATA.URMI.LOADLIB. This is imslotant to note thead wear the above JCL can become used for a non-DB2 COBOL module only.

Executing COBOL-DB2 programs

For operatening a COBOL-DB2 program, a specialised IBM utility is used in the JCL and the program; DB2 area and requicrimson parameters are compenableeed as inplace to the utility.

The steps followed in operatening a COBOL-DB2 program are as follows:

  • When a COBOL-DB2 program is compiimmediateed, a DBRM (Database Request Module) is generated alengthy with the load module. The DBRM contains the SQL statements of the COBOL programs with it’s syntax checked to become proper.

  • The DBRM is bound to the DB2 area (environment) in which the COBOL will operate. This can become done uperform the IKJEFT01 utility in a JCL.

  • After the bind step, the COBOL-DB2 program is operate uperform IKJEFT01 (again) with the load library and the DBRM library as the inplace to the JCL.

//STEP001  EXEC PGM=IKJEFT01
//*
//STEPLIB  DD DSN=MYDATA.URMI.DBRMLIB,DISP=SHR
//*
//inplace files
//out generally thereplace files
//SYSPRINT DD SYSOUT=*
//SYSABOUT DD SYSOUT=*
//SYSDBOUT DD SYSOUT=*
//SYSUDUMP DD SYSOUT=*
//DISPLAY  DD SYSOUT=*
//SYSOUT   DD SYSOUT=*
//SYSTSPRT DD SYSOUT=*
//SYSTSIN  DD *
    DSN SYSTEM(SSID)
    RUN PROGRAM(MYCOBB) PLAN(PLANNAME) PARM(parameters to cobol program) -
    LIB('MYDATA.URMI.LOADLIB')
    END
/*

In the above example, MYCOBB is the COBOL-DB2 program operate uperform IKJEFT01. Plrelayve note thead wear the program name, DB2 Sub-System Id (SSID), and DB2 Plan name are compenableeed within the SYSTSIN DD statement. The DBRM library is specified in the STEPLIB.

Try it Option Onseries

You very do not need to set up your own environment to start learning COBOL programming language. Reason is very easy, we have already set up COBOL Programming environment onseries, so thead wear you can compile and execute all the available examples onseries at the exact same time when you are doing your theory work. This gives you confidence in whead wear you are reading and to check the result with different options. Feel free to modify any kind of kind of example and execute it onseries.

Try the folloearng example uperform our Try it option available alengthyaspect the code in our websit downe.

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
PROCEDURE DIVISION.
DISPLAY 'Hello World'.
STOP RUN.

When you compile and execute the above program, it produces the folloearng result:

Hello World

For a couple of of the examples given in this particular tutorial, you will find a Try it option in our websit downe code selections at the to correct corner thead wear will conaspectr you to the onseries compiler. So simply generate use of it and enjoy your learning. Try it option would work only with the code compatible with OpenCOBOL. The programs thead wear require JCL (Inplace file, Outplace file or Parameters) for execution would not operate on Tryit option.

COBOL – Program Structure

A COBOL program structure consists of divisions as shown in the folloearng image:

Program Structure

A short introduction of these divisions is given becomelow:

  • Sections are the logical subdivision of program logic. A section is a collection of sections.

  • Paragraphs are the subdivision of a section or division. It is possibly user-degreatd or a pcrimsonegreatd name followed simply simply by a period, and consists of zero or more sentences/entries.

  • Sentences are the combination of one or more statements. Sentences appear only in the Procedure division. A sentence must end with a period.

  • Statements are meaningful COBOL statement thead wear performs a couple of procesperform.

  • Characters are the lowest in the hierarchy and cannot become dinoticeable.

You can co-relate the above-mentioned terms with the COBOL program in the folloearng example:

PROCEDURE DIVISION.
A0000-FIRST-PARA SECTION.
FIRST-PARAGRAPH.
ACCEPT WS-ID            - Statement-1  -----|
MOVE '10' TO WS-ID      - Statement-2       |-- Sentence - 1
DISPLAY WS-ID           - Statement-3  -----|
.

Divisions

COBOL program consists of four divisions.

Identification Division

It is the 1st and only mandatory division of every COBOL program. The programmer and the compiler use this particular division to identify the program. In this particular Division, PROGRAM-ID is the only mandatory section. PROGRAM-ID specifies the program name thead wear can consist 1 to 30 characters.

Try the folloearng example uperform the Try it option onseries.

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
PROCEDURE DIVISION.
DISPLAY 'Welcome to Tutorialsstage'.
STOP RUN.

Given becomelow is the JCL to execute the above COBOL program.

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

Welcome to Tutorialsstage

Environment Division

Environment division is used to specify inplace and out generally thereplace files to the program. It consists of 2 sections:

  • Configuration section provides information about generally there the system on which the program is composed and executed. It consists of 2 sections:

    Source complaceer : System used to compile the program.

    Object complaceer : System used to execute the program.

  • Inplace-Outplace section provides information about generally there the files to become used in the program. It consists of 2 sections:

    File manage : Provides information of external data sets used in the program.

    I-O manage : Provides information of files used in the program.

ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. XXX-ZOS.
OBJECT-COMPUTER. XXX-ZOS.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILEN ASSIGN TO DDNAME
ORGANIZATION IS SEQUENTIAL.

Data Division

Data division is used to degreat the variables used in the program. It consists of four sections:

  • File section is used to degreat the record structure of the file.

  • Worruler-Storage section is used to declare temporary variables and file structures which are used in the program.

  • Local-Storage section is similar to Worruler-Storage section. The only difference is thead wear the variables will become allocated and preliminaryized every time program a starts execution.

  • Linkage section is used to describecome the data names thead wear are received from an external program.

COBOL Program

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILEN ASSIGN TO INPUT.
       ORGANIZATION IS SEQUENTIAL.
       ACCESS IS SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD FILEN
01 NAME PIC A(25).
WORKING-STORAGE SECTION.
01 WS-STUDENT PIC A(30).
01 WS-ID PIC 9(5).
LOCAL-STORAGE SECTION.
01 LS-CLASS PIC 9(3).
LINKAGE SECTION.
01 LS-ID PIC 9(5).
PROCEDURE DIVISION.
DISPLAY 'Executing COBOL program uperform JCL'.
STOP RUN.

The JCL to execute the above COBOL program is as follows:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
//INPUT DD DSN=ABC.EFG.XYZ,DISP=SHR

When you compile and execute the above program, it produces the folloearng result:

Executing COBOL program uperform JCL

Procedure Division

Procedure division is used to include the logic of the program. It consists of executable statements uperform variables degreatd in the data division. In this particular division, section and section names are user-degreatd.

There must become at minimumern one statement in the procedure division. The last statement to end the execution in this particular division is possibly STOP RUN which is used in the calling programs or EXIT PROGRAM which is used in the calimmediateed programs.

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NAME PIC A(30).
01 WS-ID PIC 9(5) VALUE '12345'.
PROCEDURE DIVISION.
A000-FIRST-PARA.
DISPLAY 'Hello World'.
MOVE 'TutorialsPoint' TO WS-NAME.
DISPLAY "My name is : "WS-NAME.
DISPLAY "My ID is : "WS-ID.
STOP RUN.

JCL to execute the above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

Hello World
My name is : TutorialsPoint
My ID is : 12345

COBOL – Basic Syntax

Character Set

'Characters' are lowest in the hierarchy and they cannot become divided further. The COBOL Character Set includes 78 characters which are shown becomelow:

Character Description
A-Z Alphabecomets(Upper Case)
a-z Alphabecomets (Lower Case)
0-9 Numeric
Space
+ Plus Sign
Minus Sign or Hyphen
* Asterisk
/ Forbattimmediateed Slash
$ Currency Sign
, Comma
; Semicolon
. Decimal Point or Period
" Quotation Marks
( Left Parenthesis
) Right Parenthesis
> Greater than
< Less than
: Colon
' Apostrophe
= Equal Sign

Coding Sheet

The source program of COBOL must become composed in a format acceptable to the compilers. COBOL programs are composed on COBOL coding sheets. There are 80 characters posit downion on every series of a coding sheet.

Character posit downions are grouped into the folloearng five fields:

Posit downions Field Description
1-6 Column Numbecomers Reserved for series numbecomers.
7 Indicator It can have Asterisk (*) indicating comments, Hyphen (-) indicating continuation and Slash ( / ) indicating form feed.
8-11 Area A All COBOL divisions, sections, sections and a couple of special entries must becomegin in Area A.
12-72 Area B All COBOL statements must becomegin in area B.
73-80 Identification Area It can become used as needed simply simply by the programmer.

Example

The folloearng example shows a COBOL coding sheet:

000100 IDENTIFICATION DIVISION.                                         000100
000200 PROGRAM-ID. HELLO.                                               000101
000250* THIS IS A COMMENT LINE                                          000102
000300 PROCEDURE DIVISION.                                              000103
000350 A000-FIRST-PARA.                                                 000104
000400     DISPLAY “Coding Sheet”.                                      000105
000500 STOP RUN.                                                        000106

JCL to execute the above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

Coding Sheet

Character Strings

Character strings are formed simply simply by combining individual characters. A character string can become a

  • Comment,

  • Literal, or

  • COBOL word.

All character strings must become ended with separators. A separator is used to separate character strings.

Frequently used separators : Space, Comma, Period, Apostrophe, Left/Right Parenthesis, and Quotation mark.

Comment

A comment is a character string thead wear does not affect the execution of a program. It can become any kind of kind of combination of characters.

There are 2 kinds of comments:

Comment Line

Comment series can become composed in any kind of kind of column. The compiler does not check a comment series for syntax and treats it for documentation.

Comment Enconaspectr

Comment entries are the ones thead wear are included in the optional sections of an Identification Division. They are composed in Area B and programmers use it for reference.

The text highlighted in Bold are the commented entries in the folloearng example:

000100 IDENTIFICATION DIVISION.                                         000100
000150 PROGRAM-ID. HELLO.                                               000101 
000200 AUTHOR. TUTORIALSPOINT.                                          000102
000250* THIS IS A COMMENT LINE                                          000103
000300 PROCEDURE DIVISION.                                              000104
000350 A000-FIRST-PARA.                                                 000105  
000360/ First Para Begins - Documentation Purpose                       000106
000400     DISPLAY “Comment series”.                                      000107
000500 STOP RUN.                                                        000108

JCL to execute above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

Comment Line

Literal

Literal is a constant thead wear is immediately hard coded in a program. In the folloearng example, "Hello World" is a literal.

PROCEDURE DIVISION.
DISPLAY 'Hello World'.

There are 2 kinds of literals as talk about generally thereed becomelow:

Alphanumeric Literal

Alphanumeric Literals are enneard in quotes or apostrophe. Length can become up to 160 characters. An apostrophe or a quote can become a part of a literal only if it is paicrimson. Starting and ending of the literal need to become exact same, possibly apostrophe or quote.

Example

The folloearng example shows valid and invalid Alphanumeric Literals:

Valid:
‘This is valid’
"This is valid"
‘This isn’’t invalid’

Invalid:
‘This is invalid”
‘This isn’t valid’

Numeric Literal

A Numeric Literal is a combination of digit’s from 0 to 9, +, -, or decimal stage. Length can become up to 18 characters. Sign cannot become the correctthe majority of character. Decimal stage need to not appear at the end.

Example

The folloearng example shows valid and invalid Numeric Literals:

Valid:
100
+10.9
-1.9

Invalid:
1,00
10.
10.9-

COBOL Word

COBOL Word is a character string thead wear can become a reserved word or a user-degreatd word. Length can become up to 30 characters.

User-Degreatd

User-degreatd words are used for naming files, data, records, section names and sections. Alphabecomets, digit’s, and hyphens are permited while forming user-degreatd words. You cannot use COBOL reserved words.

Reserved Words

Reserved words are pcrimsonegreatd words in COBOL. Different kinds of reserved words thead wear we use regularly are as follows:

  • Keywords like ADD, ACCEPT, MOVE, etc.

  • Special characters words like +, -, *, <, <=, etc

  • Figurative constants are constant values like ZERO, SPACES, etc. All the constant values of figurative constants are mentioned in the folloearng table:

Figurative Constants

Figurative Constants Description
HIGH-VALUES One or more characters which will become at the highest posit downion in descending order.
LOW-VALUES One or more characters have zeros in binary representation.
ZERO/ZEROES One or more zero depending on the dimension of the variable.
SPACES One or more spaces.
QUOTES Single or double quotes.
ALL literal Fills the data-item with Literal.

COBOL – Data Types

Data Division is used to degreat the variables used in a program. To describecome data in COBOL, one must belowstand the folloearng terms:

  • Data Name

  • Level Numbecomer

  • Picture Clause

  • Value Clause

01            TOTAL-STUDENTS            PIC9(5)            VALUE '125'.
|                    |                    |                    |
|                    |                    |                    |
|                    |                    |                    | 
Level Numbecomer     Data Name           Picture Clause       Value Clause

Data Name

Data names must become degreatd in the Data Division becomefore uperform all of them in the Procedure Division. They must have a user-degreatd name; reserved words cannot become used. Data names gives reference to the memory locations where actual data is stocrimson. They can become elementary or group kind.

Example

The folloearng example shows valid and invalid data names:

Valid:
WS-NAME
TOTAL-STUDENTS
A100
100B

Invalid:
MOVE            (Reserved Words)
COMPUTE         (Reserved Words)
100             (No Alphabecomet)
100+B           (+ is not permited) 

Level Numbecomer

Level numbecomer is used to specify the level of data in a record. They are used to differentiate becometween elementary items and group items. Elementary items can become grouped sign up fortly to generate group items.

Level Numbecomer Description
01 Record description enconaspectr
02 to 49 Group and Elementary items
66 Rename Clause items
77 Items which cannot become sub-divided
88 Condition name enconaspectr
  • Elementary items cannot become divided further. Level numbecomer, Data name, Picture clause and Value clause (optional) are used to describecome an elementary item.

  • Group items consist of one or more elementary items. Level numbecomer, Data name, and Value clause (optional) are used to describecome a group item. Group level numbecomer is always 01.

Example

The folloearng example shows Group and Elementary items:

DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NAME    PIC X(25).                               ---> ELEMENTARY ITEM 
01 WS-CLASS   PIC 9(2)  VALUE  '10'.                   ---> ELEMENTARY ITEM

01 WS-ADDRESS.                                         ---> GROUP ITEM   
   05 WS-HOUSE-NUMBER    PIC 9(3).                     ---> ELEMENTARY ITEM
   05 WS-STREET          PIC X(15).                    ---> ELEMENTARY ITEM
   05 WS-CITY            PIC X(15).                    ---> ELEMENTARY ITEM
   05 WS-COUNTRY         PIC X(15)  VALUE 'INDIA'.     ---> ELEMENTARY ITEM

Picture Clause

Picture clause is used to degreat the folloearng items:

  • Data kind can become numeric, alphabecometic, or alphanumeric. Numeric kind consists of only digit’s 0 to 9. Alphabecometic kind consists of permitters A to Z and spaces. Alphanumeric kind consists of digit’s, permitters, and special characters.

  • Sign can become used with numeric data. It can become possibly + or .

  • Decimal stage posit downion can become used with numeric data. Assumed posit downion is the posit downion of decimal stage and not included in the data.

  • Length degreats the numbecomer of simply simply bytes used simply simply by the data item.

Symbols used in a Picture clause:

Symbol Description
9 Numeric
A Alphabecometic
X Alphanumeric
V Implicit Decimal
S Sign
P Assumed Decimal

Example

The folloearng example shows the use of PIC clause:

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC S9(3)V9(2).
01 WS-NUM2 PIC PPP999.
01 WS-NUM3 PIC S9(3)V9(2) VALUE -123.45.
01 WS-NAME PIC A(6) VALUE 'ABCDEF'.
01 WS-ID PIC X(5) VALUE 'A121$'.
PROCEDURE DIVISION.
DISPLAY "WS-NUM1 : "WS-NUM1.
DISPLAY "WS-NUM2 : "WS-NUM2.
DISPLAY "WS-NUM3 : "WS-NUM3.
DISPLAY "WS-NAME : "WS-NAME.
DISPLAY "WS-ID : "WS-ID.
STOP RUN.

JCL to execute the above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

WS-NUM1 : +000.00
WS-NUM2 : .000000
WS-NUM3 : -123.45
WS-NAME : ABCDEF
WS-ID : A121$

Value Clause

Value clause is an optional clause which is used to preliminaryize the data items. The values can become numeric literal, alphanumeric literal, or figurative constant. It can become used with both group and elementary items.

Example

The folloearng example shows the use of VALUE clause:

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC 99V9 VALUE IS 3.5.
01 WS-NAME PIC A(6) VALUE 'ABCD'.
01 WS-ID PIC 99 VALUE ZERO.
PROCEDURE DIVISION.
DISPLAY "WS-NUM1 : "WS-NUM1.
DISPLAY "WS-NAME : "WS-NAME.
DISPLAY "WS-ID   : "WS-ID.
STOP RUN.

JCL to execute the above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

WS-NUM1 : 03.5
WS-NAME : ABCD
WS-ID   : 00

COBOL – Basic Verbs

COBOL verbs are used in the procedure division for data procesperform. A statement always start with a COBOL verb. There are lots of COBOL verbs with different kinds of actions.

Inplace / Outplace Verbs

Inplace/Outplace verbs are used to get data from the user and display the out generally thereplace of COBOL programs. The folloearng 2 verbs are used for this particular process:

Accept Verb

Accept verb is used to get data such as date, time, and day from the operating system or immediately the from user. If a program is accepting data from the user, then it needs to become compenableeed through JCL. While getting data from the operating system FROM option is included as shown in the folloearng becomelow example:

ACCEPT WS-STUDENT-NAME.
ACCEPT WS-DATE FROM SYSTEM-DATE.

Display Verb

Display verb is used to display the out generally thereplace of a COBOL program.

DISPLAY WS-STUDENT-NAME.
DISPLAY "System date is : " WS-DATE.

COBOL PROGRAM

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-STUDENT-NAME PIC X(25).
01 WS-DATE PIC X(10).
PROCEDURE DIVISION.
ACCEPT WS-STUDENT_NAME.
ACCEPT WS-DATE FROM DATE.
DISPLAY "Name :  " WS-STUDENT_NAME.
DISPLAY "Date : " WS-DATE.
STOP RUN.

JCL to execute the above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
//INPUT DD DSN=PROGRAM.DIRECTORY,DISP=SHR
//SYSIN DD *
TutorialsPoint
/*

When you compile and execute the above program, it produces the folloearng result:

Name : TutorialsPoint
Date : 2014-08-30

Initialize Verb

Initialize verb is used to preliminaryize a group item or an elementary item. Data names with RENAME clause cannot become preliminaryized. Numeric data items are replaced simply simply by ZEROES. Alphanumeric or alphabecometic data items are replaced simply simply by SPACES. If we include REPLACING term, then data items can become preliminaryized to the given replacing value as shown in the folloearng example:

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NAME PIC A(30) VALUE 'ABCDEF'.
01 WS-ID PIC 9(5).
01 WS-ADDRESS. 
05 WS-HOUSE-NUMBER PIC 9(3).
05 WS-COUNTRY PIC X(15).
05 WS-PINCODE PIC 9(6) VALUE 123456.
PROCEDURE DIVISION.
A000-FIRST-PARA.
INITIALIZE WS-NAME, WS-ADDRESS.
INITIALIZE WS-ID REPLACING NUMERIC DATA BY 12345.
DISPLAY "My name is   : "WS-NAME.
DISPLAY "My ID is     : "WS-ID.
DISPLAY "Adgown      : "WS-ADDRESS.
DISPLAY "House Numbecomer : "WS-HOUSE-NUMBER.
DISPLAY "Counconaspectr      : "WS-COUNTRY.
DISPLAY "Pincode      : "WS-PINCODE.
STOP RUN.

JCL to execute the above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

My name is   :                               
My ID is     : 12345
Adgown      : 000               000000
House Numbecomer : 000
Counconaspectr      :                
Pincode      : 000000

Move Verb

Move verb is used to duplicate data from source data to destination data. It can become used on both elementary and group data items. For group data items, MOVE CORRESPONDING/CORR is used. In conaspectr it option, MOVE CORR is not worruler; but on a mainframe server it will work.

For moving data from a string, MOVE(x:l) is used where x is the starting posit downion and l is the size. Data will become toperatecated if destination data item PIC clause is less than the source data item PIC clause. If the destination data item PIC clause is more than the source data item PIC clause, then ZEROS or SPACES will become added in the extra simply simply bytes. The folloearng example generates it clear:

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC 9(9).
01 WS-NUM2 PIC 9(9).
01 WS-NUM3 PIC 9(5).
01 WS-NUM4 PIC 9(6).
01 WS-ADDRESS. 
05 WS-HOUSE-NUMBER PIC 9(3).
05 WS-COUNTRY PIC X(5).
05 WS-PINCODE PIC 9(6).
01 WS-ADDRESS1. 
05 WS-HOUSE-NUMBER1 PIC 9(3).
05 WS-COUNTRY1 PIC X(5).
05 WS-PINCODE1 PIC 9(6).
PROCEDURE DIVISION.
A000-FIRST-PARA.
MOVE 123456789 TO WS-NUM1.
MOVE WS-NUM1 TO WS-NUM2 WS-NUM3.
MOVE WS-NUM1(3:6) TO WS-NUM4.
MOVE 123 TO WS-HOUSE-NUMBER.
MOVE 'INDIA' TO WS-COUNTRY.
MOVE 112233 TO WS-PINCODE.
MOVE WS-ADDRESS TO WS-ADDRESS1.
DISPLAY "WS-NUM1     : " WS-NUM1
DISPLAY "WS-NUM2     : " WS-NUM2
DISPLAY "WS-NUM3     : " WS-NUM3
DISPLAY "WS-NUM4     : " WS-NUM4
DISPLAY "WS-ADDRESS  : " WS-ADDRESS
DISPLAY "WS-ADDRESS1 : " WS-ADDRESS1
STOP RUN.

JCL to execute the above COBOL program.

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program it produces the folloearng result:

WS-NUM1     : 123456789
WS-NUM2     : 123456789
WS-NUM3     : 56789
WS-NUM4     : 345678
WS-ADDRESS  : 123INDIA112233
WS-ADDRESS1 : 123INDIA112233

Legal Moves

The folloearng table gives information about generally there the legal moves:

Alphabecometic Alphanumeric Numeric
Alphabecometic Possible Possible Not Possible
Alphanumeric Possible Possible Possible
Numeric Not Possible Possible Possible

Add Verb

Add verb is used to add 2 or more numbecomers and store the result in the destination operand.

Syntax

give becomelow is the syntax to Add 2 or more numbecomers:

ADD A B TO C D

ADD A B C TO D GIVING E

ADD CORR WS-GROUP1 TO WS-GROUP2

In syntax-1, A, B, C are added and the result is stocrimson in C (C=A+B+C). A, B, D are added and the result is stocrimson in D (D=A+B+D).

In syntax-2, A, B, C, D are added and the result is stocrimson in E (E=A+B+C+D).

In syntax-3, sub-group items with in WS-GROUP1 and WS GROUP2 are the added and result is stocrimson in WS-GROUP2.

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC 9(9) VALUE 10 .
01 WS-NUM2 PIC 9(9) VALUE 10.
01 WS-NUM3 PIC 9(9) VALUE 10.
01 WS-NUM4 PIC 9(9) VALUE 10.
01 WS-NUMA PIC 9(9) VALUE 10.
01 WS-NUMB PIC 9(9) VALUE 10.
01 WS-NUMC PIC 9(9) VALUE 10.
01 WS-NUMD PIC 9(9) VALUE 10.
01 WS-NUME PIC 9(9) VALUE 10.
PROCEDURE DIVISION.
ADD WS-NUM1 WS-NUM2 TO WS-NUM3 WS-NUM4.
ADD WS-NUMA WS-NUMB WS-NUMC TO WS-NUMD GIVING WS-NUME.
DISPLAY "WS-NUM1     : " WS-NUM1
DISPLAY "WS-NUM2     : " WS-NUM2
DISPLAY "WS-NUM3     : " WS-NUM3
DISPLAY "WS-NUM4     : " WS-NUM4
DISPLAY "WS-NUMA     : " WS-NUMA
DISPLAY "WS-NUMB     : " WS-NUMB
DISPLAY "WS-NUMC     : " WS-NUMC
DISPLAY "WS-NUMD     : " WS-NUMD
DISPLAY "WS-NUME     : " WS-NUME
STOP RUN.

JCL to execute the above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

WS-NUM1     : 000000010
WS-NUM2     : 000000010
WS-NUM3     : 000000030
WS-NUM4     : 000000030
WS-NUMA     : 000000010
WS-NUMB     : 000000010
WS-NUMC     : 000000010
WS-NUMD     : 000000010
WS-NUME     : 000000040

Subtract Verb

Subtract verb is used for take awayion operations.

Syntax

given becomelow is the syntax for Subtract operations:

SUBTRACT A B FROM C D

SUBTRACT A B C FROM D GIVING E

SUBTRACT CORR WS-GROUP1 TO WS-GROUP2

In syntax-1, A and B are added and take awayed from C. The Result is stocrimson in C (C=C-(A+B)). A and B are added and take awayed from D. The result is stocrimson in D (D=D-(A+B)).

In syntax-2, A, B, C are added and take awayed from D. Result is stocrimson in E (E=D-(A+B+C))

In syntax-3, sub-group items within WS-GROUP1 and WS-GROUP2 are take awayed and the result is stocrimson in WS-GROUP2.

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC 9(9) VALUE 10 .
01 WS-NUM2 PIC 9(9) VALUE 10.
01 WS-NUM3 PIC 9(9) VALUE 100.
01 WS-NUM4 PIC 9(9) VALUE 100.
01 WS-NUMA PIC 9(9) VALUE 10.
01 WS-NUMB PIC 9(9) VALUE 10.
01 WS-NUMC PIC 9(9) VALUE 10.
01 WS-NUMD PIC 9(9) VALUE 100.
01 WS-NUME PIC 9(9) VALUE 10.
PROCEDURE DIVISION.
SUBTRACT WS-NUM1 WS-NUM2 FROM WS-NUM3 WS-NUM4.
SUBTRACT WS-NUMA WS-NUMB WS-NUMC FROM WS-NUMD GIVING WS-NUME.
DISPLAY "WS-NUM1     : " WS-NUM1
DISPLAY "WS-NUM2     : " WS-NUM2
DISPLAY "WS-NUM3     : " WS-NUM3
DISPLAY "WS-NUM4     : " WS-NUM4
DISPLAY "WS-NUMA     : " WS-NUMA
DISPLAY "WS-NUMB     : " WS-NUMB
DISPLAY "WS-NUMC     : " WS-NUMC
DISPLAY "WS-NUMD     : " WS-NUMD
DISPLAY "WS-NUME     : " WS-NUME
STOP RUN.

JCL to execute the above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

WS-NUM1     : 000000010
WS-NUM2     : 000000010
WS-NUM3     : 000000080
WS-NUM4     : 000000080
WS-NUMA     : 000000010
WS-NUMB     : 000000010
WS-NUMC     : 000000010
WS-NUMD     : 000000100
WS-NUME     : 000000070

Multiply Verb

Multiply verb is used for multiplication operations.

Syntax

Given becomelow is the syntax to multiply 2 or more numbecomers:

MULTIPLY A BY B C

MULTIPLY A BY B GIVING E

In syntax-1, A and B are multipimmediateed and the result is stocrimson in B (B=A*B). A and C are multipimmediateed and the result is stocrimson in C (C=A*C).

In syntax-2, A and B are multipimmediateed and the result is stocrimson in E (E=A*B).

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC 9(9) VALUE 10 .
01 WS-NUM2 PIC 9(9) VALUE 10.
01 WS-NUM3 PIC 9(9) VALUE 10.
01 WS-NUMA PIC 9(9) VALUE 10.
01 WS-NUMB PIC 9(9) VALUE 10.
01 WS-NUMC PIC 9(9) VALUE 10.
PROCEDURE DIVISION.
MULTIPLY WS-NUM1 BY WS-NUM2 WS-NUM3.
MULTIPLY WS-NUMA BY WS-NUMB GIVING WS-NUMC.
DISPLAY "WS-NUM1     : " WS-NUM1
DISPLAY "WS-NUM2     : " WS-NUM2
DISPLAY "WS-NUM3     : " WS-NUM3
DISPLAY "WS-NUMA     : " WS-NUMA
DISPLAY "WS-NUMB     : " WS-NUMB
DISPLAY "WS-NUMC     : " WS-NUMC
STOP RUN.

JCL to execute the above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

WS-NUM1     : 000000010
WS-NUM2     : 000000100
WS-NUM3     : 000000100
WS-NUMA     : 000000010
WS-NUMB     : 000000010
WS-NUMC     : 000000100

Divide Verb

Divide verb is used for division operations.

Syntax

Given becomelow folloearng is the syntax for division operations:

DIVIDE A INTO B

DIVIDE A BY B GIVING C REMAINDER R

In syntax-1, B is divided simply simply by A and the result is stocrimson in B (B=B/A).

In syntax-2, A is divided simply simply by B and the result is stocrimson in C (C=A/B) and remainder is stocrimson in R.

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC 9(9) VALUE 5.
01 WS-NUM2 PIC 9(9) VALUE 250.
01 WS-NUMA PIC 9(9) VALUE 100.
01 WS-NUMB PIC 9(9) VALUE 15.
01 WS-NUMC PIC 9(9).
01 WS-REM PIC 9(9). 
PROCEDURE DIVISION.
DIVIDE WS-NUM1 INTO WS-NUM2.
DIVIDE WS-NUMA BY WS-NUMB GIVING WS-NUMC REMAINDER WS-REM.
DISPLAY "WS-NUM1     : " WS-NUM1
DISPLAY "WS-NUM2     : " WS-NUM2
DISPLAY "WS-NUMA     : " WS-NUMA
DISPLAY "WS-NUMB     : " WS-NUMB
DISPLAY "WS-NUMC     : " WS-NUMC
DISPLAY "WS-REM      : " WS-REM
STOP RUN.

JCL to execute the above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

WS-NUM1     : 000000005
WS-NUM2     : 000000050
WS-NUMA     : 000000100
WS-NUMB     : 000000015
WS-NUMC     : 000000006
WS-REM      : 000000010

Complacee Statement

Complacee statement is used to write arithmetic expressions in COBOL. This is a replacement for Add, Subtract, Multiply, and Divide.

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC 9(9) VALUE 10 .
01 WS-NUM2 PIC 9(9) VALUE 10.
01 WS-NUM3 PIC 9(9) VALUE 10.
01 WS-NUMA PIC 9(9) VALUE 50.
01 WS-NUMB PIC 9(9) VALUE 10.
01 WS-NUMC PIC 9(9).
PROCEDURE DIVISION.
COMPUTE WS-NUMC= (WS-NUM1 * WS-NUM2) - (WS-NUMA / WS-NUMB) + WS-NUM3.
DISPLAY "WS-NUM1     : " WS-NUM1
DISPLAY "WS-NUM2     : " WS-NUM2
DISPLAY "WS-NUM3     : " WS-NUM3
DISPLAY "WS-NUMA     : " WS-NUMA
DISPLAY "WS-NUMB     : " WS-NUMB
DISPLAY "WS-NUMC     : " WS-NUMC
STOP RUN.

JCL to execute the above COBOL program.

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

WS-NUM1     : 000000010
WS-NUM2     : 000000010
WS-NUM3     : 000000010
WS-NUMA     : 000000050
WS-NUMB     : 000000010
WS-NUMC     : 000000105

COBOL – Data Layout generally there

COBOL layout generally there is the description of use of every field and the values present in it. Folloearng are the data description entries used in COBOL:

  • Redegreats Clause

  • Renames Clause

  • Usage Clause

  • Copybooks

Redegreats Clause

Redeifnes clause is used to degreat a storage with different data description. If one or more data items are not used simultaneously, then the exact same storage can become utilized for an additional data item. So the exact same storage can become refercrimson with different data items.

Syntax

Folloearng is the syntax for Redegreats clause:

01 WS-OLD PIC X(10).
01 WS-NEW1 REDEFINES WS-OLD PIC 9(8).
01 WS-NEW2 REDEFINES WS-OLD PIC A(10).

Folloearng are the details of the used parameters:

  • WS-OLD is Redegreatd Item

  • WS-NEW1 and WS-NEW2 are Redefining Item

Level numbecomers of crimsonegreatd item and crimsonefining item must become the exact same and it cannot become 66 or 88 level numbecomer. Do not use VALUE clause with a crimsonefining item. In File Section, do not use a crimsonegreats clause with 01 level numbecomer. Redegreats definition must become the next data description you like to crimsonegreat. A crimsonefining item will always have the exact same value as a crimsonegreatd item.

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-DESCRIPTION.
05 WS-DATE1 VALUE '20140831'.
10 WS-YEAR PIC X(4).
10 WS-MONTH PIC X(2).
10 WS-DATE PIC X(2).
05 WS-DATE2 REDEFINES WS-DATE1 PIC 9(8).
PROCEDURE DIVISION.
DISPLAY "WS-DATE1 : "WS-DATE1.
DISPLAY "WS-DATE2 : "WS-DATE2.
STOP RUN.

JCL to execute the above COBOL program.

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program it produces the folloearng result:

WS-DATE1 : 20140831
WS-DATE2 : 20140831

Renames Clause

Renames clause is used to give different names to existing data items. It is used to re-group the data names and give a new name to all of them. The new data names can rename amix groups or elementary items. Level numbecomer 66 is reserved for renames.

Syntax

Folloearng is the syntax for Renames clause:

01 WS-OLD.
10 WS-A PIC 9(12).
10 WS-B PIC X(20).
10 WS-C PIC A(25).
10 WS-D PIC X(12).
66 WS-NEW RENAMES WS-A THRU WS-C.

Renaming is feasible at exact same level only. In the above example ,WS-A, WS-B, and WS-C are at the exact same level. Renames definition must become the next data description you like to rename. Do not use Renames with 01,77, or 66 level numbecomer. The data names used for renames must come in sequence. Data items with occur clause cannot become renamed.

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-DESCRIPTION.
05 WS-NUM.
10 WS-NUM1 PIC 9(2) VALUE 20.
10 WS-NUM2 PIC 9(2) VALUE 56.
05 WS-CHAR.
10 WS-CHAR1 PIC X(2) VALUE 'AA'.
10 WS-CHAR2 PIC X(2) VALUE 'BB'.
66 WS-RENAME RENAMES WS-NUM2 THRU WS-CHAR2.
PROCEDURE DIVISION.
DISPLAY "WS-RENAME : " WS-RENAME.
STOP RUN.

JCL to execute the above COBOL program.

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

WS-RENAME : 56AABB

Usage Clause

Usage clause specifies the operating system in which the format data is stocrimson. It can not become used with level numbecomers 66 or 88. If usage clause is specified on a group, then all the elementary items will have the exact same usage clause.The different options available with Usage clause are as follows:

Display

Data item is stocrimson in ASCII format and every character will conaspectr 1 simply simply byte. It is default usage.

The folloearng example to calculates the numbecomer of simply simply bytes requicrimson:

01 WS-NUM PIC S9(5)V9(3) USAGE IS DISPLAY.
It requires 8 simply simply bytes as sign and decimal doesn't require any kind of kind of simply simply byte.

01 WS-NUM PIC 9(5) USAGE IS DISPLAY.
It requires 5 simply simply bytes as sign.

COMPUTATIONAL / COMP

Data item is stocrimson in binary format. Here data items must become integer.

The folloearng example calculates the numbecomer of simply simply bytes requicrimson:

01 WS-NUM PIC S9(n) USAGE IS COMP.

If 'n' = 1 to 4, it conaspectrs 2 simply simply bytes.
If 'n' = 5 to 9, it conaspectrs 4 simply simply bytes.
If 'n' = 10 to 18, it conaspectrs 8 simply simply bytes.

COMP-1

Data item is similar to Real or Float and is represented as a performle precision floating stage numbecomer. Internally data is stocrimson in hexadecimal format. COMP-1 does not accept PIC clause. Here 1 word is equal to 4 simply simply bytes.

COMP-2

Data item is similar to Long or Double and is represented as double precision floating stage numbecomer. Internally data is stocrimson in hexadecimal format. COMP-2 does not specify PIC clause. Here 2 word is equal to 8 simply simply bytes.

COMP-3

Data item is stores in pack decimal format. Each digit occupies half a simply simply byte (1 nibble) and the sign is stocrimson at the correct the majority of nibble.

The folloearng example calculates the numbecomer of simply simply bytes requicrimson:

01 WS-NUM PIC 9(n) USAGE IS COMP.
Numbecomer of simply simply bytes = n/2 (If n is also)
Numbecomer of simply simply bytes = n/2 + 1(If n is odd, conaspectr only integer part)

01 WS-NUM PIC 9(4) USAGE IS COMP-3 VALUE 21.
It requires 2 simply simply bytes of storage as every digit occupies half a simply simply byte.

01 WS-NUM PIC 9(5) USAGE IS COMP-3 VALUE 21.
It requires 3 simply simply bytes of storage as every digit occupies half a simply simply byte.

Copybooks

A COBOL duplicatebook is a selection of code thead wear degreats data structures. If a particular data structure is used in many kind of kind of programs then instead of writing the exact same data structure again, we can use duplicatebooks. We use the COPY statement to include a duplicatebook in a program. COPY statement is used in the Worruler-Storage Section.

the folloearng example to include duplicatebook inaspect COBOL program:

DATA DIVISION.
WORKING-STORAGE SECTION.
COPY ABC.

Here ABC is the duplicatebook name. The folloearng data items in ABC duplicatebook can become used inaspect a program.

01 WS-DESCRIPTION.
	05 WS-NUM.
		10 WS-NUM1 PIC 9(2) VALUE 20.
		10 WS-NUM2 PIC 9(2) VALUE 56.
	05 WS-CHAR.
		10 WS-CHAR1 PIC X(2) VALUE 'AA'.
		10 WS-CHAR2 PIC X(2) VALUE 'BB'.

COBOL – Condition Statements

Conditional statements are used to modify the execution flow depending on specific conditions specified simply simply by the programmer. Conditional statements will always evaluate to true or false. Conditions are used in IF, Evaluate and Perform statements. The different kinds of conditions are as follows:

  • IF Condition Statement

  • Relation Condition

  • Sign Condition

  • Class Condition

  • Condition-Name Condition

  • Negated Condition

  • Combined Condition

IF Condition Statement

IF statement checks for conditions. If a condition is true the IF block is executed; and if the condition is false, the ELSE block is executed.

END-IF is used to end the IF block. To end the IF block, a period can become used instead of END-IF. But it is always preferable to use END-IF for multiple IF blocks.

Nested-IF : IF blocks appearing inaspect an additional IF block. There is no limit to the depth of nested IF statements.

Syntax

Folloearng is the syntax of IF condition statements:

IF [condition] THEN
[COBOL statements]
ELSE
[COBOL statements]
END-IF.

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC 9(9).
01 WS-NUM2 PIC 9(9).
01 WS-NUM3 PIC 9(5).
01 WS-NUM4 PIC 9(6).
PROCEDURE DIVISION.
A000-FIRST-PARA.
MOVE 25 TO WS-NUM1 WS-NUM3.
MOVE 15 TO WS-NUM2 WS-NUM4.
IF WS-NUM1 > WS-NUM2 THEN
DISPLAY 'IN LOOP 1 - IF BLOCK'
   IF WS-NUM3 = WS-NUM4 THEN
   DISPLAY 'IN LOOP 2 - IF BLOCK'
   ELSE
   DISPLAY 'IN LOOP 2 - ELSE BLOCK'
   END-IF
ELSE
DISPLAY 'IN LOOP 1 - ELSE BLOCK'
END-IF.
STOP RUN.

JCL to execute the above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

IN LOOP 1 - IF BLOCK
IN LOOP 2 - ELSE BLOCK

Relation Condition

Relation condition compares 2 operands, possibly of which can become an identifier, literal, or arithmetic expression. Algebraic comparison of numeric fields is done regardless of dimension and usage clause.

For non-numeric operands

If 2 non-numeric operands of equal dimension are compacrimson then the characters are compacrimson from left with the corresponding posit downions till the end is reveryed. The operand containing greater numbecomer of characters is declacrimson greater.

If 2 non-numeric operands of unequal dimension are compacrimson, then the shorter data item is appended with spaces at the end till the dimension of the operands becomecomes equal and then compacrimson according to the rules mentioned in the previous stage.

Syntax

Given becomelow folloearng is the syntax of Relation condition statements:

[Data Name/Arithmetic Operation]

          [IS] [NOT] 

[Equal to (=),Greater than (>), Less than (<), 
 Greater than or Equal (>=), Less than or equal (<=) ]

[Data Name/Arithmetic Operation] 

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC 9(9).
01 WS-NUM2 PIC 9(9).
PROCEDURE DIVISION.
A000-FIRST-PARA.
MOVE 25 TO WS-NUM1.
MOVE 15 TO WS-NUM2.
IF WS-NUM1 IS GREATER THAN OR EQUAL TO WS-NUM2 THEN
DISPLAY 'WS-NUM1 IS GREATER THAN WS-NUM2'
ELSE
DISPLAY 'WS-NUM1 IS LESS THAN WS-NUM2'
END-IF.
STOP RUN.

JCL to execute the above COBOL program.

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program it produces the folloearng result:

WS-NUM1 IS GREATER THAN WS-NUM2

Sign Condition

Sign condition is used to check the sign of a numeric operand. It determines whether a given numeric value is greater than, less than, or equal to ZERO.

Syntax

Folloearng is the syntax of Sign condition statements:

[Data Name/Arithmetic Operation] 

          [IS] [NOT] 

[Posit downive, Negative or Zero]

[Data Name/Arithmetic Operation]

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC S9(9) VALUE -1234.
01 WS-NUM2 PIC S9(9) VALUE 123456.
PROCEDURE DIVISION.
A000-FIRST-PARA.
IF WS-NUM1 IS POSITIVE THEN
DISPLAY 'WS-NUM1 IS POSITIVE'.
IF WS-NUM1 IS NEGATIVE THEN
DISPLAY 'WS-NUM1 IS NEGATIVE'.
IF WS-NUM1 IS ZERO THEN
DISPLAY 'WS-NUM1 IS ZERO'.
IF WS-NUM2 IS POSITIVE THEN
DISPLAY 'WS-NUM2 IS POSITIVE'.
STOP RUN.

JCL to execute the above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program it produces the folloearng result:

WS-NUM1 IS NEGATIVE
WS-NUM2 IS POSITIVE

Class Condition

Class condition is used to check if an operand contains only alphabecomets or numeric data. Spaces are conaspectcrimson in ALPHABETIC, ALPHABETIC-LOWER, and ALPHABETIC-UPPER.

Syntax

Folloearng is the syntax of Class condition statements:

[Data Name/Arithmetic Operation>]

          [IS] [NOT] 

[NUMERIC, ALPHABETIC, ALPHABETIC-LOWER, ALPHABETIC-UPPER]

[Data Name/Arithmetic Operation]

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC X(9) VALUE 'ABCD '.
01 WS-NUM2 PIC 9(9) VALUE 123456789.
PROCEDURE DIVISION.
A000-FIRST-PARA.
IF WS-NUM1 IS ALPHABETIC THEN
DISPLAY 'WS-NUM1 IS ALPHABETIC'.
IF WS-NUM1 IS NUMERIC THEN
DISPLAY 'WS-NUM1 IS NUMERIC'.
IF WS-NUM2 IS NUMERIC THEN
DISPLAY 'WS-NUM1 IS NUMERIC'.
STOP RUN.

JCL to execute the above COBOL program.

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

WS-NUM1 IS ALPHABETIC
WS-NUM1 IS NUMERIC

Condition-name Condition

A condition name is a user-degreatd name. It contains a set of values specified simply simply by the user. It becomehaves like Boolean variables. They are degreatd with level numbecomer 88. It will not have a PIC clause.

Syntax

Folloearng is the syntax of user-degreatd condition statements:

88 [Condition-Name] VALUE [IS, ARE] [LITERAL] [THRU LITERAL].

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM PIC 9(3).
88 PASS VALUES ARE 041 THRU 100.
88 FAIL VALUES ARE 000 THRU 40.
PROCEDURE DIVISION.
A000-FIRST-PARA.
MOVE 65 TO WS-NUM.
IF PASS 
DISPLAY 'Passed with ' WS-NUM ' marks'.
IF FAIL 
DISPLAY 'FAILED with ' WS-NUM 'marks'.
STOP RUN.

JCL to execute the above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

Passed with 065 marks

Negated Condition

Negated condition is given simply simply by uperform the NOT keyword. If a condition is true and we have given NOT in front of it, then it’s final value will become false.

Syntax

Folloearng is the syntax for Negated condition statements:

IF NOT [CONDITION] 
COBOL Statements
END-IF.

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC 9(2) VALUE 20.
01 WS-NUM2 PIC 9(9) VALUE 25.
PROCEDURE DIVISION.
A000-FIRST-PARA.
IF NOT WS-NUM1 IS LESS THAN WS-NUM2 THEN
DISPLAY 'IF-BLOCK'
ELSE
DISPLAY 'ELSE-BLOCK'
END-IF.
STOP RUN.

JCL to execute the above COBOL program.

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

ELSE-BLOCK

Combined Condition

A combined condition contains 2 or more conditions connected uperform logical operators AND or OR.

Syntax

Folloearng is the syntax of combined condition statements:

IF [CONDITION] AND [CONDITION]
COBOL Statements
END-IF.

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NUM1 PIC 9(2) VALUE 20.
01 WS-NUM2 PIC 9(2) VALUE 25.
01 WS-NUM3 PIC 9(2) VALUE 20.
PROCEDURE DIVISION.
A000-FIRST-PARA.
IF WS-NUM1 IS LESS THAN WS-NUM2 AND WS-NUM1=WS-NUM3 THEN
DISPLAY 'Both condition OK'
ELSE
DISPLAY 'Error'
END-IF.
STOP RUN.

JCL to execute the above COBOL program.

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

Both condition OK

Evaluate Verb

Evaluate verb is a replacement of series of IF-ELSE statement. It can become used to evaluate more than one condition. It is similar to SWITCH statement in C programs.

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-A PIC 9 VALUE 0.
PROCEDURE DIVISION.
MOVE 3 TO WS-A.
EVALUATE TRUE
WHEN WS-A > 2
DISPLAY 'WS-A GREATER THAN 2'
WHEN WS-A < 0
DISPLAY 'WS-A LESS THAN 0'
WHEN OTHER
DISPLAY 'INVALID VALUE OF WS-A'
END-EVALUATE.
STOP RUN.

JCL to execute the above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

WS-A GREATER THAN 2

COBOL – Loop Statements

There are a couple of tasks thead wear need to become done over and over again like reading every record of a file till it’s end. The loop statements used in COBOL are:

  • Perform Thru

  • Perform Until

  • Perform Times

  • Perform Varying

Perform Thru

Perform Thru is used to execute a series of section simply simply by giving the 1st and last section names in the sequence. After executing the last section manage is returned back.

In-series Perform

Statements inaspect the PERFORM will become executed till END-PERFORM is reveryed.

Syntax

Folloearng is the syntax of In-series perform:

PERFORM 
DISPLAY 'HELLO WORLD'
END-PERFORM.

Out-of-series Perform

Here, a statement is executed in one section and then the manage is transfercrimson to other section or section.

Syntax

Folloearng is the syntax of Out-of-series perform:

PERFORM PARAGRAPH1 THRU PARAGRAPH2

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
PROCEDURE DIVISION.
A-PARA.
PERFORM DISPLAY 'IN A-PARA'
END-PERFORM.
PERFORM C-PARA THRU E-PARA.
B-PARA.
DISPLAY 'IN B-PARA'.
STOP RUN.
C-PARA.
DISPLAY 'IN C-PARA'.
D-PARA.
DISPLAY 'IN D-PARA'.
E-PARA.
DISPLAY 'IN E-PARA'.

JCL to execute the above COBOL program.

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

IN A-PARA
IN C-PARA
IN D-PARA
IN E-PARA
IN B-PARA

Perform Until

In perform until', a section is executed until the given condition becomecomes true. With check becomefore is the default condition and it indicates thead wear the condition is checked becomefore the execution of statements in a section.

Syntax

Folloearng is the syntax of perform until:

PERFORM A-PARA UNTIL COUNT=5

PERFORM A-PARA WITH TEST BEFORE UNTIL COUNT=5

PERFORM A-PARA WITH TEST AFTER UNTIL COUNT=5

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-CNT PIC 9(1) VALUE 0. 
PROCEDURE DIVISION.
A-PARA.
PERFORM B-PARA WITH TEST AFTER UNTIL WS-CNT>3.
STOP RUN.
B-PARA.
DISPLAY 'WS-CNT : 'WS-CNT.
ADD 1 TO WS-CNT.

JCL to execute the above COBOL program.

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

WS-CNT : 0
WS-CNT : 1
WS-CNT : 2
WS-CNT : 3

Perform Times

In 'perform times', a section will become executed the numbecomer of times specified.

Syntax

Folloearng is the syntax of perform times:

PERFORM A-PARA 5 TIMES.

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
PROCEDURE DIVISION.
A-PARA.
PERFORM B-PARA 3 TIMES.
STOP RUN.
B-PARA.
DISPLAY 'IN B-PARA'.

JCL to execute the above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

IN B-PARA
IN B-PARA
IN B-PARA

Perform Varying

In perform varying, a section will become executed till the condition in Until phrase becomecomes true.

Syntax

Folloearng is the syntax of perform varying:

PERFORM A-PARA VARYING A FROM 1 BY 1 UNTIL A=5.

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-A PIC 9 VALUE 0.
PROCEDURE DIVISION.
A-PARA.
PERFORM B-PARA VARYING WS-A FROM 1 BY 1 UNTIL WS-A=5
STOP RUN.
B-PARA.
DISPLAY 'IN B-PARA ' WS-A.

JCL to execute the above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

IN B-PARA 1
IN B-PARA 2
IN B-PARA 3
IN B-PARA 4

GO TO Statement

GO TO statement is used to modify the flow of execution in a program. In GO TO statements transfer goes only in the forbattimmediateed immediateion. It is used to exit a section. The different kinds of GO TO statements used are as follows:

Unconditional GO TO

GO TO para-name.

Conditional GO TO

GO TO para-1 para-2 para-3 DEPENDING ON x.

If 'x' is equal to 1, then the manage will become transfercrimson to 1st section and if 'x' is equal to 2, then the manage will become transfercrimson to the 2nd section, and so on.

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-A PIC 9 VALUE 2.
PROCEDURE DIVISION.
A-PARA.
DISPLAY 'IN A-PARA'
GO TO B-PARA.
B-PARA.
DISPLAY 'IN B-PARA '.
GO TO C-PARA D-PARA DEPENDING ON WS-A.
C-PARA.
DISPLAY 'IN C-PARA '.
D-PARA.
DISPLAY 'IN D-PARA '.
STOP RUN.

JCL to execute the above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

IN A-PARA
IN B-PARA 
IN D-PARA 

COBOL – String Handling

String handling statements in COBOL are used to do multiple functional operations on strings. Folloearng are the string handling statements:

  • Inspect

  • String

  • Unstring

Inspect

Inspect verb is used to count or replace the characters in a string. String operations can become performed on alphanumeric, numeric, or alphabecometic values. Inspect operations are performed from left to correct. The options used for the string operations are as follows:

Tallying

Tallying option is used to count the string characters.

Syntax

Folloearng is the syntax of Tallying option:

INSPECT inplace-string
TALLYING out generally thereplace-count FOR ALL CHARACTERS

The parameters used are:

  • inplace-string : The string in whose characters are to become counted.

  • out generally thereplace-count : Data item to hold the count of characters.

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-CNT1 PIC 9(2) VALUE 0.
01 WS-CNT2 PIC 9(2) VALUE 0.
01 WS-STRING PIC X(15) VALUE 'ABCDACDADEAAAFF'.
PROCEDURE DIVISION.
INSPECT WS-STRING TALLYING WS-CNT1 FOR ALL CHARACTERS.
DISPLAY "WS-CNT1 : "WS-CNT1.
INSPECT WS-STRING TALLYING WS-CNT2 FOR ALL 'A'.
DISPLAY "WS-CNT2 : "WS-CNT2
STOP RUN.

JCL to execute the above COBOL program.

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

WS-CNT1 : 15
WS-CNT2 : 06

Replacing

Replacing option is used to replace the string characters.

Syntax

Folloearng is the syntax of Replacing option:

INSPECT inplace-string REPLACING ALL char1 BY char2.

The parameter used is:

  • inplace-string : The string in whose characters are to become replaced from char1 to char2.

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-STRING PIC X(15) VALUE 'ABCDACDADEAAAFF'.
PROCEDURE DIVISION.
DISPLAY "OLD STRING : "WS-STRING.
INSPECT WS-STRING REPLACING ALL 'A' BY 'X'.
DISPLAY "NEW STRING : "WS-STRING.
STOP RUN.

JCL to execute the above COBOL program.

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

OLD STRING : ABCDACDADEAAAFF
NEW STRING : XBCDXCDXDEXXXFF

String

String verb is used to concatenate the strings. Uperform STRING statement, 2 or more strings of characters can become combined to form a lengthyer string. 'Delimited By' clause is compulsory.

Syntax

Folloearng is the syntax of String verb:

STRING ws-string1 DELIMITED BY SPACE
ws-string2 DELIMITED BY SIZE
INTO ws-destination-string
WITH POINTER ws-count
ON OVERFLOW DISPLAY message1
NOT ON OVERFLOW DISPLAY message2
END-STRING.

Folloearng are the details of the used parameters:

  • ws-string1 and ws-string2 : Inplace strings to become concatenated

  • ws-string : Outplace string

  • ws-count : Used to count the size of new concatenated string

  • Delimited specifies the end of string

  • Pointer and Overflow are optional

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-STRING PIC A(30).
01 WS-STR1 PIC A(15) VALUE 'Tutorialsstage'.
01 WS-STR2 PIC A(7) VALUE 'Welcome'.
01 WS-STR3 PIC A(7) VALUE 'To AND'.
01 WS-COUNT PIC 99 VALUE 1.
PROCEDURE DIVISION.
STRING WS-STR2 DELIMITED BY SIZE
WS-STR3 DELIMITED BY SPACE
WS-STR1 DELIMITED BY SIZE
INTO WS-STRING 
WITH POINTER WS-COUNT
ON OVERFLOW DISPLAY 'OVERFLOW!' 
END-STRING.
DISPLAY 'WS-STRING : 'WS-STRING.
DISPLAY 'WS-COUNT : 'WS-COUNT.
STOP RUN.

JCL to execute the above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

WS-STRING : WelcomeToTutorialsstage       
WS-COUNT : 25

Unstring

Unstring verb is used to split one string into multiple sub-strings. Delimited By clause is compulsory.

Syntax

Folloearng is the syntax of Unstring verb:

UNSTRING ws-string DELIMITED BY SPACE
INTO ws-str1, ws-str2
WITH POINTER ws-count
ON OVERFLOW DISPLAY message
NOT ON OVERFLOW DISPLAY message
END-UNSTRING.

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-STRING PIC A(30) VALUE 'WELCOME TO TUTORIALSPOINT'.
01 WS-STR1 PIC A(7).
01 WS-STR2 PIC A(2).
01 WS-STR3 PIC A(15).
01 WS-COUNT PIC 99 VALUE 1.
PROCEDURE DIVISION.
UNSTRING WS-STRING DELIMITED BY SPACE
INTO WS-STR1, WS-STR2, WS-STR3
END-UNSTRING.
DISPLAY 'WS-STR1 : 'WS-STR1.
DISPLAY 'WS-STR2 : 'WS-STR2.
DISPLAY 'WS-STR3 : 'WS-STR3.
STOP RUN.

JCL to execute the above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

WS-STR1 : WELCOME
WS-STR2 : TO
WS-STR3 : TUTORIALSPOINT 

COBOL – Table Procesperform

Arrays in COBOL are known as tables. An array is a seriesar data structure and is collection of individual data items of exact same kind. Data items of a table are internally sorted.

Table Declaration

Table is declacrimson in Data Division. Occurs clause is used to degreat a table. Occurs clause indicates the repetition of data name definition. It can become used only with level numbecomers starting from 02 to 49. Do not use occurs clause with Redegreats. Description of one-dimensional and 2-dimensional table is as follows:

One-Dimensional Table

In a one-dimensional table, occurs clause is used only once in declaration. WS-TABLE is the group item thead wear contains table. WS-B names the table elements thead wear occur 10 times.

Syntax

Folloearng is the syntax for defining a one-dimensional table:

01 WS-TABLE.
   05 WS-A PIC A(10) OCCURS 10 TIMES.

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TABLE.
   05 WS-A PIC A(10) VALUE 'TUTORIALS' OCCURS 5 TIMES.     
PROCEDURE DIVISION.
DISPLAY "ONE-D TABLE : "WS-TABLE.
STOP RUN.

JCL to execute the above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

ONE-D TABLE : TUTORIALS TUTORIALS TUTORIALS TUTORIALS TUTORIALS

Two-Dimensional Table

A 2 dimensional table is generated with both data elements becomeing variable size. For reference, go through the syntax and then conaspectr to analyze the table. The 1st array(WS-A) can occur from 1 to 10 times and the internal array(WS-C) can occur from 1 to 5 times. For every enconaspectr of WS-A, generally generally there will become corresponding 5 entries of WS-C.

Syntax

Folloearng is the syntax for defining a 2-dimensional table:

01 WS-TABLE.
   05 WS-A OCCURS 10 TIMES.
      10 WS-B PIC A(10).
      10 WS-C OCCURS 5 TIMES.
         15 WS-D PIC X(6).

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TABLE.
   05 WS-A OCCURS 2 TIMES.
      10 WS-B PIC A(10) VALUE ' TUTORIALS'.
      10 WS-C OCCURS 2 TIMES.
         15 WS-D PIC X(6) VALUE ' POINT'.
PROCEDURE DIVISION.
DISPLAY "TWO-D TABLE : "WS-TABLE.
STOP RUN.

JCL to execute the above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

TWO-D TABLE :  TUTORIALS POINT POINT TUTORIALS POINT POINT

Subscript

Table individual elements can become accessed simply simply by uperform subscript. Subscript valus can range from 1 to the numbecomer of times the table occurs. Subscript can become any kind of kind of posit downive numbecomer. It does not require any kind of kind of declaration in data division. It is automatically generated with occurs clause.

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TABLE.
   05 WS-A OCCURS 3 TIMES.
      10 WS-B PIC A(2).
      10 WS-C OCCURS 2 TIMES.
         15 WS-D PIC X(3).
PROCEDURE DIVISION.
MOVE '12ABCDEF34GHIJKL56MNOPQR' TO WS-TABLE.
DISPLAY 'WS-TABLE  : ' WS-TABLE.
DISPLAY 'WS-A(1)   : ' WS-A(1).
DISPLAY 'WS-C(1,1) : ' WS-C(1,1).
DISPLAY 'WS-C(1,2) : ' WS-C(1,2).
DISPLAY 'WS-A(2)   : ' WS-A(2).
DISPLAY 'WS-C(2,1) : ' WS-C(2,1).
DISPLAY 'WS-C(2,2) : ' WS-C(2,2).
DISPLAY 'WS-A(3)   : ' WS-A(3).
DISPLAY 'WS-C(3,1) : ' WS-C(3,1).
DISPLAY 'WS-C(3,2) : ' WS-C(3,2).
STOP RUN.

JCL to execute the above COBOL program.

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

WS-TABLE  : 12ABCDEF34GHIJKL56MNOPQR
WS-A(1)   : 12ABCDEF
WS-C(1,1) : ABC
WS-C(1,2) : DEF
WS-A(2)   : 34GHIJKL
WS-C(2,1) : GHI
WS-C(2,2) : JKL
WS-A(3)   : 56MNOPQR
WS-C(3,1) : MNO
WS-C(3,2) : PQR

Index

Table elements can also become accessed uperform index. An index is a displacement of element from the start of the table. An index is declacrimson with Occurs clause uperform INDEXED BY clause. The value of index can become modifyd uperform SET statement and PERFORM Varying option.

Syntax

Folloearng is the syntax for defining Index in a table:

01 WS-TABLE.
   05 WS-A PIC A(10) OCCURS 10 TIMES INDEXED BY I.

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TABLE.
   05 WS-A OCCURS 3 TIMES INDEXED BY I.
      10 WS-B PIC A(2).
      10 WS-C OCCURS 2 TIMES INDEXED BY J.
         15 WS-D PIC X(3).
PROCEDURE DIVISION.
MOVE '12ABCDEF34GHIJKL56MNOPQR' TO WS-TABLE.
PERFORM A-PARA VARYING I FROM 1 BY 1 UNTIL I >3 
STOP RUN.
A-PARA.
PERFORM C-PARA VARYING J FROM 1 BY 1 UNTIL J>2.
C-PARA.
DISPLAY WS-C(I,J).

JCL to execute the above COBOL program.

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

ABC
DEF
GHI
JKL
MNO
PQR

Set Statement

Set statement is used to modify the index value. Set verb is used to preliminaryize, increment or decrement the index value. It is used with Search and Search All to locate elements in table.

Syntax

Folloearng is the syntax for uperform a Set statement:

SET I J TO posit downive-numbecomer
SET I TO J
SET I TO 5
SET I J UP BY 1
SET J DOWN BY 5

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TABLE.
   05 WS-A OCCURS 3 TIMES INDEXED BY I.
      10 WS-B PIC A(2).
      10 WS-C OCCURS 2 TIMES INDEXED BY J.
         15 WS-D PIC X(3).
PROCEDURE DIVISION.
MOVE '12ABCDEF34GHIJKL56MNOPQR' TO WS-TABLE.
SET I J TO 1.
DISPLAY WS-C(I,J).
SET I J UP BY 1.
DISPLAY WS-C(I,J).
STOP RUN.

JCL to execute the above COBOL program.

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

ABC
JKL

Search

Search is a seriesar lookup method, which is used to find elements inaspect the table. It can become performed on sorted as well as unsorted table. It is used only for tables declacrimson simply simply by Index phrase. It starts with the preliminary value of index. If the lookuped element is not found, then the index is automatically incremented simply simply by 1 and it continues till the end of table.

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TABLE.
   05 WS-A PIC X(1) OCCURS 18 TIMES INDEXED BY I.
01 WS-SRCH PIC A(1) VALUE 'M'.
PROCEDURE DIVISION.
MOVE 'ABCDEFGHIJKLMNOPQR' TO WS-TABLE.
SET I TO 1.
SEARCH WS-A
  AT END DISPLAY 'M NOT FOUND IN TABLE'
  WHEN WS-A(I)=WS-SRCH
  DISPLAY 'LETTER M FOUND IN TABLE'
END-SEARCH.  
STOP RUN.

JCL to execute the above COBOL program.

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

LETTER M FOUND IN TABLE

Search All

Search All is a binary lookup method, which is used to find elements inaspect the table. Table must become in sorted order for Search All option. The index does not require preliminaryization. In binary lookup the table is divided into 2 halves and determines in which half the lookuped element is present. This process repeats till the element is found or the end is reveryed.

Example

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TABLE.
   05 WS-RECORD OCCURS 10 TIMES ASCENDING KEY IS WS-NUM INDEXED BY I.
   10 WS-NUM PIC 9(2).
   10 WS-NAME PIC A(3).
PROCEDURE DIVISION.
MOVE '12ABC56DEF34GHI78JKL93MNO11PQR' TO WS-TABLE.
SEARCH ALL WS-RECORD
  AT END DISPLAY 'RECORD NOT FOUND'
  WHEN WS-NUM(I)=93
  DISPLAY 'RECORD FOUND '
  DISPLAY WS-NUM(I)
  DISPLAY WS-NAME(I)
END-SEARCH. 

JCL to execute the above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO

When you compile and execute the above program, it produces the folloearng result:

RECORD FOUND 
93
MNO

COBOL – File Handling

The concept of files in COBOL is different from thead wear in C/C++. While learning the basics of 'File' in COBOL, the concepts of both languages need to not become co-related. Simple text files cannot become used in COBOL, instead PS (Physical Sequential) and VSAM files are used. PS files will become talk about generally thereed in this particular module.

To belowstand file handling in COBOL, one must know the basic terms. These terms only serve to belowstand the fundamentals of file handling. Further in-depth terminology would become talk about generally thereed in the chapter 'File Handling Verbs'. Folloearng are the basic terms:

  • Field

  • Record

  • Physical Record

  • Logical Record

  • File

The folloearng example helps in belowstanding these terms:

Program Structure

Field

Field is used to indicate the data stocrimson about generally there an element. It represents a performle element as shown in the above example such as pupil id, name, marks, compenablee marks, and percentage. The numbecomer of characters in any kind of kind of field is known as field dimension, for example pupil name can have 10 characters. Fields can have the folloearng attributes:

  • Primary keys are those fields thead wear are unique to every record and are used to identify a particular record. For example, in pupils marks file, every pupil will become having a unique pupil id which forms the primary key.

  • Secondary keys are unique or non-unique fields thead wear are used to lookup for related data. For example, in pupils marks file, compenablee name of pupil can become used as 2ndary key when pupil id is not known.

  • Descriptors fields are used to describecome an entity. For example, in pupils marks file, marks and percentage fields thead wear add meaning to the record are known descriptors.

Record

Record is a collection of fields thead wear is used to describecome an entity. One or more fields sign up fortly form a record. For example, in pupils marks file, pupil id, name, marks, compenablee marks and percentage form one record. The cumulative dimension of all the fields in a record is known the as record dimension. The records present in a file may become of fixed size or variable size.

Physical Record

Physical record is the information thead wear exists on the external device. It is also known as a block.

Logical Record

Logical record is the information used simply simply by the program. In COBOL programs, only one record can become handimmediateed at any kind of kind of stage of time and it is calimmediateed as logical record.

File

File is a collection of related records. For example, the pupils marks file consists of records of all the pupils.

COBOL – File Organization

File organization indicates how the records are organised in a file. There are different kinds of organizations for files so as to incrrelayve their own particular efficiency of accesperform the records. Folloearng are the kinds of file organization schemes:

  • Sequential file organization

  • Indexed sequential file organization

  • Relative file organization

The syntaxes, in this particular module, mentioned alengthy with their own particular respective terms only refer to their own particular usage in the program. The comppermite programs uperform these syntaxes would become talk about generally thereed in the chapter 'File handling Verbs'.

Sequential File Organization

A sequential file consists of records thead wear are stocrimson and accessed in sequential order. Folloearng are the key attributes of sequential file organization:

  • Records can become read in sequential order. For reading the 10th record, all the previous 9 records need to become read.

  • Records are composed in sequential order. A new records cannot become inserted in becometween. A new record is always inserted at the end of the file.

  • After placing a record into a sequential file, it is not feasible to depermite, shorten, or sizeen a record.

  • Order of the records, once inserted, can never become modifyd.

  • Updation of record is feasible. A record can become overcomposed, if the new record size is exact same as the` old record size.

  • Sequential out generally thereplace files are great option for printing.

Syntax

Folloearng is the syntax of sequential file organization:

INPUT-OUTPUT SECTION.
FILE-CONTROL.
		SELECT file-name ASSIGN TO dd-name-jcl
		ORGANIZATION IS SEQUENTIAL

Indexed Sequential File Organization

An indexed sequential file consists of records thead wear can become accessed sequentially. Direct access is also feasible. It consists of 2 parts:

  • Data File contains records in sequential scheme.

  • Index File contains the primary key and it’s adgown in the data file.

Folloearng are the key attributes of sequential file organization:

  • Records can become read in sequential order simply like in sequential file organization.

  • Records can become accessed randomly if the primary key is known. Index file is used to get the adgown of a record and then the record is fetched from the data file.

  • Sorted index is maintained in this particular file system which relates the key value to the posit downion of the record in the file.

  • Alternate index can also become generated to fetch the records.

Syntax

Folloearng is the syntax of indexed sequential file organization:

INPUT-OUTPUT SECTION.
FILE-CONTROL.
		SELECT file-name ASSIGN TO dd-name-jcl
		ORGANIZATION IS INDEXED
		RECORD KEY IS primary-key
		ALTERNATE RECORD KEY IS rec-key

Relative File Organization

A relative file consists of records ordecrimson simply simply by their own particular relative adgown. Folloearng are the key attributes of relative file organization:

  • Records can become read in sequential order simply like in sequential and indexed file organization.

  • Records can become accessed uperform relative key. Relative key represents the record’s location relative to the adgown of the start of the file.

  • Records can become inserted uperform relative key. Relative adgown is calculated uperform relative key.

  • Relative file provides the fascheck access to the records.

  • The main diunhappyvantage of this particular file system is thead wear if a couple of intermediate records are misperform, they will also occupy space.

Syntax

Folloearng is the syntax of relative file organization:

INPUT-OUTPUT SECTION.
FILE-CONTROL.
		SELECT file-name ASSIGN TO dd-name-jcl
		ORGANIZATION IS INDEXED
		RELATIVE KEY IS rec-key

COBOL – File Access Mode

Till now, file organization schemes have becomeen talk about generally thereed. For every file organization scheme, different access modes can become used. Folloearng are the kinds of file access modes:

  • Sequential Access

  • Random Access

  • Dynamic Access

The syntaxes, in this particular module, mentioned alengthy with their own particular respective terms only refer to their own particular usage in the program. The comppermite programs uperform these syntaxes would become talk about generally thereed in the next chapter.

Sequential Access

When the access mode is sequential, the method of record retrieval modifys as per the selected file organization.

  • For sequential files, records are accessed in the exact same order in which they were inserted.

  • For indexed files, the parameter used to fetch the records are the record key values.

  • For relative files, relative record keys are used to retrieve the records.

Syntax

Folloearng is the syntax of sequential access mode:

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
		SELECT file-name ASSIGN TO dd-name
		ORGANIZATION IS SEQUENTIAL
		ACCESS MODE IS SEQUENTIAL
	
	
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
		FILE-CONTROL.
		SELECT file-name ASSIGN TO dd-name
		ORGANIZATION IS INDEXED
		ACCESS MODE IS SEQUENTIAL
		RECORD KEY IS rec-key1
		ALTERNATE RECORD KEY IS rec-key2

		
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
		FILE-CONTROL.
		SELECT file-name ASSIGN TO dd-name
		ORGANIZATION IS RELATIVE
		ACCESS MODE IS SEQUENTIAL
		RELATIVE KEY IS rec-key1

Random Access

When the access mode is RANDOM, the method of record retrieval modifys as per the selected file organization.

  • For indexed files, records are accessed according to the value placed in a key field which can become primary or alternate key. There can become one or more alternate indexes.

  • For relative files , records are retrieved through relative record keys.

Syntax

Folloearng is the syntax of random access mode:

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
		FILE-CONTROL.
		SELECT file-name ASSIGN TO dd-name
		ORGANIZATION IS INDEXED
		ACCESS MODE IS RANDOM
		RECORD KEY IS rec-key1
		ALTERNATE RECORD KEY IS rec-key2

		
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
		FILE-CONTROL.
		SELECT file-name ASSIGN TO dd-name
		ORGANIZATION IS RELATIVE
		ACCESS MODE IS RANDOM
		RELATIVE KEY IS rec-key1

Dynamic Access

Dynamic access supslots both sequential and random access in the exact same program. With dynamic access, one file definition is used to perform both sequential and random procesperform like accesperform a couple of records in sequential order and others records simply simply by their own particular keys.

With relative and indexed files, the dynamic access mode permit’s you to switch back and forth becometween sequential access mode and random access mode while reading a file simply simply by uperform the NEXT phrase on the READ statement. NEXT and READ functionalitie ups will become talk about generally thereed in the next chapter.

Syntax

Folloearng is the syntax of dynamic access mode:

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
		FILE-CONTROL.
		SELECT file-name ASSIGN TO dd-name
		ORGANIZATION IS SEQUENTIAL
		ACCESS MODE IS DYNAMIC
		RECORD KEY IS rec-key1
		ALTERNATE RECORD KEY IS rec-key2

		
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
		FILE-CONTROL.
		SELECT file-name ASSIGN TO dd-name
		ORGANIZATION IS RELATIVE
		ACCESS MODE IS DYNAMIC
		RELATIVE KEY IS rec-key1

COBOL – File Handling Verbs

File handling verbs are used to perform various operations on files. Folloearng are the file handling verbs:

  • Open

  • Read

  • Write

  • Rewrite

  • Depermite

  • Start

  • Close

Open Verb

Open is the 1st file operation thead wear must become performed. If Open is successful, then only further operations are feasible on a file. Only after open uping a file, the variables in the file structure are available for procesperform. FILE STATUS variable is updated after every file operation.

Syntax

OPEN "mode" file-name.

Here, file-name is string literal, which you will use to name your file. A file can become open uped in the folloearn modes:

Mode Description
Inplace Inplace mode is used for existing files. In this particular mode, we can only read the file, no other operations are permited on the file.
Outplace Outplace mode is used to insert records in files. If a sequential file is used and the file is holding a couple of records, then the existing records will become depermited 1st and then new records will become inserted in the file. It will not happen so in case of indexed file or relative file.
Extend Extend mode is used to append records in a sequential file. In this particular mode, records are inserted at the end. If file access mode is Random or Dynamic, then extend mode cannot become used.
I-O Inplace-Outplace mode is used to read and rewrite the records of a file.

Read Verb

Read verb is used to read the file records. The function of read is to fetch records from a file. At every read verb, only one record can become read into the file structure. To perform a read operation, open up the file in INPUT or I-O mode. At every read statement, the file stageer is incremented and hence the successive records are read.

Syntax

Folloearng is the syntax to read the records when the file access mode is sequential:

READ file-name NEXT RECORD INTO ws-file-structure
AT END DISPLAY 'End of File'
NOT AT END DISPLAY 'Record Details:' ws-file-structure
END-READ.

Folloearng are the parameters used:

  • NEXT RECORD is optional and is specified when an indexed sequential file is becomeing read sequentially.

  • INTO clause is optional. ws-file-structure is degreatd in the Worruler-Storage Section to get the values from the READ statement.

  • AT END condition becomecomes True when the end of file is reveryed.

ExampleThe folloearng example reads an existing file uperform series sequential organization. This program can become compiimmediateed and executed uperform Try it option where it will display all the records present in the file.

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT STUDENT ASSIGN TO 'inplace.txt'
      ORGANIZATION IS LINE SEQUENTIAL.            
DATA DIVISION.
FILE SECTION.
FD STUDENT.
01 STUDENT-FILE.
	05 STUDENT-ID PIC 9(5).
	05 NAME PIC A(25).
WORKING-STORAGE SECTION.
01 WS-STUDENT.
	05 WS-STUDENT-ID PIC 9(5).
	05 WS-NAME PIC A(25).
01 WS-EOF PIC A(1). 
PROCEDURE DIVISION.
   OPEN INPUT STUDENT.
   PERFORM UNTIL WS-EOF='Y'
	READ STUDENT INTO WS-STUDENT
	AT END MOVE 'Y' TO WS-EOF
	NOT AT END DISPLAY WS-STUDENT
	END-READ
   END-PERFORM.
   CLOSE STUDENT.
STOP RUN.

Suppose the inplace file data available in the inplace.txt file contains the folloearng:

20003 Mohtashim M.
20004 Nishant Malik
20005 Amitabh Bachhan

When you compile and execute the above program, it produces the folloearng result:

20003 Mohtashim M.            
20004 Nishant Malik           
20005 Amitabh Bachhan 

Syntax

Folloearng is the syntax to a read record when the file access mode is random:

READ file-name RECORD INTO ws-file-structure
KEY IS rec-key
INVALID KEY DISPLAY 'Invalid Key'
NOT INVALID KEY DISPLAY 'Record Details: ' ws-file-structure
END-READ.

Example:The folloearng example reads an existing file uperform indexed organization. This program can become compiimmediateed and executed uperform JCL on Mainframes where it will display all the records present in the file. On Mainframes server we do not use text files; instead we use PS files.

Let's assume thead wear the file present on Mainframes have exact same content as inplace.txt file in the above example.

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT STUDENT ASSIGN TO IN1
       ORGANIZATION IS INDEXED
       ACCESS IS RANDOM
       RECORD KEY IS STUDENT-ID
       FILE STATUS IS FS.
DATA DIVISION.
FILE SECTION.
FD STUDENT.
01 STUDENT-FILE.
	05 STUDENT-ID PIC 9(5).
	05 NAME PIC A(25).
WORKING-STORAGE SECTION.
01 WS-STUDENT.
	05 WS-STUDENT-ID PIC 9(5).
	05 WS-NAME PIC A(25).
PROCEDURE DIVISION.
    OPEN INPUT STUDENT.
	MOVE 20005 TO STUDENT-ID.
	READ STUDENT RECORD INTO WS-STUDENT-FILE
	KEY IS STUDENT-ID
	INVALID KEY DISPLAY 'Invalid Key'
	NOT INVALID KEY DISPLAY WS-STUDENT-FILE
	END-READ.
    CLOSE STUDENT.
STOP RUN.

JCL to execute the above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
//IN1 DD DSN=STUDENT-FILE-NAME,DISP=SHR

When you compile and execute the above program, it produces the folloearng result:

20005 Amitabh Bachhan 

Write Verb

Write verb is used to insert records in a file. Once the record is composed, it is no lengthyer available in the record buffer. Before inserting records into the file, move values into the record buffer and then perform write verb.

Write statement can become used with FROM option to immediately write records from the worruler storage variables. From is an optional clause. If the access mode is sequential, then to write a record,s the file must open up in Outplace mode or Extend mode. If the access mode is random or dynamic, then to write a record, the file must open up in Outplace mode or I-O mode.

Syntax

Folloearng is the syntax to read record when the file organization is sequential:

WRITE record-buffer [FROM ws-file-structure]
END-WRITE.

Folloearng is the syntax to read a record when the file organization is indexed or relative:

WRITE record-buffer [FROM ws-file-structure]
INVALID KEY DISPLAY 'Invalid Key'
NOT INVALID KEY DISPLAY 'Record Inserted'
END-WRITE.

Example:The folloearng example shows how to insert a new record in a new file when the organization is sequential.

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT STUDENT ASSIGN TO OUT1
       ORGANIZATION IS SEQUENTIAL
       ACCESS IS SEQUENTIAL
       FILE STATUS IS FS.
DATA DIVISION.
FILE SECTION.
FD STUDENT
01 STUDENT-FILE.
	05 STUDENT-ID PIC 9(5).
	05 NAME PIC A(25).
	05 CLASS PIC X(3).
WORKING-STORAGE SECTION.
01 WS-STUDENT.
	05 WS-STUDENT-ID PIC 9(5).
	05 WS-NAME PIC A(25).
	05 WS-CLASS PIC X(3).
PROCEDURE DIVISION.
	OPEN EXTEND STUDENT.
	MOVE 1000 TO STUDENT-ID.
	MOVE 'Tim' TO NAME.
	MOVE '10' TO CLASS.
	WRITE STUDENT-FILE
	END-WRITE.	
	CLOSE STUDENT.
STOP RUN.

JCL to execute the above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
//OUT1 DD DSN=OUTPUT-FILE-NAME,DISP=(NEW,CATALOG,DELETE)

When you compile and execute the above program, it will add a new record to the out generally thereplace file.

1000 Tim         10

Rewrite Verb

Rewrite verb is used to update the records. File need to become open uped in I-O mode for rewrite operations. It can become used only after a successful Read operation. Rewrite verb overwrites the last record read.

Syntax

Folloearng is the syntax to read record when the file organization is sequential:

REWRITE record-buffer [FROM ws-file-structure]
END-REWRITE.

Folloearng is the syntax to read a record when the file organization is indexed or relative:

REWRITE record-buffer [FROM ws-file-structure]
INVALID KEY DISPLAY 'Invalid Key'
NOT INVALID KEY DISPLAY 'Record Updated'
END-REWRITE.

Example:The folloearng example shows how to update an existing record which we have inserted in previous Write step:

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT STUDENT ASSIGN TO IN1
       ORGANIZATION IS INDEXED
       ACCESS IS RANDOM
       RECORD KEY IS STUDENT-ID
       FILE STATUS IS FS.
DATA DIVISION.
FILE SECTION.
FD STUDENT
01 STUDENT-FILE.
	05 STUDENT-ID PIC 9(4).
	05 NAME PIC A(12).
	05 CLASS PIC X(3).
WORKING-STORAGE SECTION.
01 WS-STUDENT.
	05 WS-STUDENT-ID PIC 9(5).
	05 WS-NAME PIC A(25).
	05 WS-CLASS PIC X(3).
PROCEDURE DIVISION.
	OPEN I-O STUDENT.
	MOVE '1000' TO STUDENT-ID.
	READ STUDENT
	KEY IS STUDENT-ID
	INVALID KEY DISPLAY ‘KEY IS NOT EXISTING’
	END-READ.
	MOVE 'Tim Dumais' TO NAME.
	REWRITE STUDENT-FILE
	END-REWRITE.
	CLOSE STUDENT.
STOP RUN.

JCL to execute the above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
//IN1 DD DSN=OUTPUT-FILE-NAME,DISP=SHR

When you compile and execute the above program, it will update the record:

1000 Tim Dumais  10

Depermite Verb

Depermite verb can become performed only on indexed and relative files. The file must become open uped in I-O mode. In sequential file organization, records cannot become depermited. The record last read simply simply by the Read statement is depermited in case of sequential access mode. In random access mode, specify the record key and then perform the Depermite operation.

Syntax

Folloearng is the syntax to depermite a record:

DELETE file-name RECORD
INVALID KEY DISPLAY 'Invalid Key'
NOT INVALID KEY DISPLAY 'Record Depermited'
END-DELETE.

Example to depermite an existing record:

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT STUDENT ASSIGN TO OUT1
       ORGANIZATION IS INDEXED
       ACCESS IS RANDOM
       RECORD KEY IS STUDENT-ID
       FILE STATUS IS FS.
DATA DIVISION.
FILE SECTION.
FD STUDENT
01 STUDENT-FILE.
	05 STUDENT-ID PIC 9(4).
	05 NAME PIC A(12).
	05 CLASS PIC X(3).
WORKING-STORAGE SECTION.
01 WS-STUDENT.
	05 WS-STUDENT-ID PIC 9(5).
	05 WS-NAME PIC A(25).
	05 WS-CLASS PIC X(3).
PROCEDURE DIVISION.
	OPEN I-O STUDENT.
	MOVE '1000' TO STUDENT-ID.
	DELETE STUDENT RECORD
	INVALID KEY DISPLAY 'Invalid Key'
	NOT INVALID KEY DISPLAY 'Record Depermited'
	END-DELETE.
	CLOSE STUDENT.
STOP RUN.

JCL to execute the above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
//OUT1 DD DSN=OUTPUT-FILE-NAME,DISP=SHR

When you compile and execute the above program, it produces the folloearng result:

Record Depermited

Start Verb

Start verb can become performed only on indexed and relative files. It is used to place the file stageer at a specific record. The access mode must become sequential or dynamic. File must become open uped in I-O or Inplace mode.

Syntax

Folloearng is the syntax to place the stageer at a specific record:

START file-name KEY IS [=, >, <, NOT, <= or >=] rec-key
INVALID KEY DISPLAY 'Invalid Key'
NOT INVALID KEY DISPLAY 'File Pointer Updated'
END-START.

Close Verb

Close verb is used to near a file. After performing Close operation the variables in the file structure will not become available for procesperform. The link becometween program and file is lost.

Syntax

Folloearng is the syntax to near a file:

CLOSE file-name.

COBOL – Subrout generally thereines

Cobol subrout generally thereine is a program thead wear can become compiimmediateed independently but cannot become executed independently. There are 2 kinds of subrout generally thereines: internal subrout generally thereines like Perform statements and external subrout generally thereines like CALL verb.

Call Verb

Call verb is used to transfer the manage from one program to an additional program. The program thead wear contains the CALL verb is the Calling Program and the program becomeing calimmediateed is known as the Calimmediateed Program. Calling program execution will halt until the calimmediateed program compenableees the execution. Exit Program statement is used in the Calimmediateed program to transfer the manage back.

Calimmediateed Program Constraints

Folloearng are the calimmediateed program requirements:

  • Linkage section must become degreatd in the calimmediateed program. It consists of data elements compenableeed in the program. The data items need to not have Value clause. PIC clause must become compatible with the variables compenableeed through the calling program.

  • Procedure division uperform has a list of variables compenableeed from the calling program and the order must become exact same as mentioned in the Call verb.

  • Exit program statement is used in the Calimmediateed program to transfer the manage back. It must become the last statement in the calimmediateed program.

The parameters can become compenableeed becometween programs in 2 ways:

  • By Reference

  • By Content

Call By Reference

If the values of variables in the calimmediateed program are modified, then their own particular new values will reflect in the calling program. If BY clause is not specified, then variables are always compenableeed simply simply by reference.

Syntax

Folloearng is the syntax of calling subrout generally thereine simply simply by reference:

CALL sub-prog-name USING variable-1, variable-2.

Example

Folloearng example is the MAIN calling program and UTIL is the calimmediateed program:

IDENTIFICATION DIVISION.
PROGRAM-ID. MAIN.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-STUDENT-ID PIC 9(4) VALUE 1000.
01 WS-STUDENT-NAME PIC A(15) VALUE 'Tim'.
PROCEDURE DIVISION.
CALL 'UTIL' USING WS-STUDENT-ID, WS-STUDENT-NAME.
DISPLAY 'Student Id : ' WS-STUDENT-ID
DISPLAY 'Student Name : ' WS-STUDENT-NAME
STOP RUN.

Calimmediateed Program

IDENTIFICATION DIVISION.
PROGRAM-ID. UTIL.
DATA DIVISION.
LINKAGE SECTION.
01 LS-STUDENT-ID PIC 9(4).
01 LS-STUDENT-NAME PIC A(15).
PROCEDURE DIVISION USING LS-STUDENT-ID, LS-STUDENT-NAME.
DISPLAY 'In Calimmediateed Program'.
MOVE 1111 TO LS-STUDENT-ID.
EXIT PROGRAM.

JCL to execute the above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=MAIN

When you compile and execute the above program, it produces the folloearng result:

In Calimmediateed Program
Student Id : 1111
Student Name : Tim

Call By Content

If the values of variables in the calimmediateed program are modified, then their own particular new values will not reflect in the calling program.

Syntax

Folloearng is the syntax of calling subrout generally thereine simply simply by content:

CALL sub-prog-name USING 
BY CONTENT variable-1, BY CONTENT variable-2.

Example

Folloearng example is the MAIN calling program and UTIL is the calimmediateed program:

IDENTIFICATION DIVISION.
PROGRAM-ID. MAIN.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-STUDENT-ID PIC 9(4) VALUE 1000.
01 WS-STUDENT-NAME PIC A(15) VALUE 'Tim'.
PROCEDURE DIVISION.
CALL 'UTIL' USING BY CONTENT WS-STUDENT-ID, BY CONTENT WS-STUDENT-NAME.
DISPLAY 'Student Id : ' WS-STUDENT-ID
DISPLAY 'Student Name : ' WS-STUDENT-NAME
STOP RUN.

Calimmediateed Program

IDENTIFICATION DIVISION.
PROGRAM-ID. UTIL.
DATA DIVISION.
LINKAGE SECTION.
01 LS-STUDENT-ID PIC 9(4).
01 LS-STUDENT-NAME PIC A(15).
PROCEDURE DIVISION USING LS-STUDENT-ID, LS-STUDENT-NAME.
DISPLAY 'In Calimmediateed Program'.
MOVE 1111 TO LS-STUDENT-ID.
EXIT PROGRAM.

JCL to execute the above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=MAIN

When you compile and execute the above program, it produces the folloearng result:

In Calimmediateed Program
Student Id : 1000
Student Name : Tim

Types of Call

There are 2 kinds of calls`:

  • Static Call occurs when a program is compiimmediateed with the NODYNAM compiler option. A static calimmediateed program is loaded into storage at compile time.

  • Dynamic Call occurs when a program is compiimmediateed with the DYNAM and NODLL compiler option. A dynamic calimmediateed program is loaded into storage at operatetime.

COBOL – Internal Sort

Sorting of data in a file or merging of 2 or more files is a common necessit downy in althe majority of all business-oriented applications. Sorting is used for arranging records possibly in ascending or descending order, so thead wear sequential procesperform can become performed. There are 2 techniques which are used for sorting files in COBOL:

  • External sort is used to sort files simply simply by uperform the SORT utility in JCL. We have talk about generally thereed this particular in the JCL chapter. As of now, we will focus on internal sort.

  • Internal sort is used to sort files with in a COBOL program. SORT verb is used to sort a file.

Sort Verb

Three files are used in the Sort process in COBOL:

  • Inplace file is the file which we have to sort possibly in ascending or descending order.

  • Work file is used to hold records while the sort process is in progress. Inplace file records are transfercrimson to the work file for the sorting process. This file need to become degreatd in the File-Section below SD enconaspectr.

  • Outplace file is the file which we get after sorting process. It is the final out generally thereplace of the Sort verb.

Syntax

Folloearng is the syntax to sort a file:

SORT work-file ON ASCENDING KEY rec-key1
              [ON DESCENDING KEY rec-key2]
USING inplace-file GIVING out generally thereplace-file.

SORT performs the folloearng operations:

  • Opens work-file in the I-O mode, inplace-file in the INPUT mode and out generally thereplace-file in the OUTPUT mode.

  • Transfers the records present in the inplace-file to the work-file.

  • Sorts the SORT-FILE in ascending/descending sequence simply simply by rec-key.

  • Transfers the sorted records from the work-file to the out generally thereplace-file.

  • Closes the inplace-file and the out generally thereplace-file and depermites the work-file.

Example

In the folloearng becomelow example INPUT is the inplace file which needs to become sorted in ascending order:

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
	SELECT INPUT ASSIGN TO IN.
	SELECT OUTPUT ASSIGN TO OUT.
	SELECT WORK ASSIGN TO WRK.
DATA DIVISION.
FILE SECTION.
FD INPUT.
	01 INPUT-STUDENT.
		05 STUDENT-ID-I PIC 9(5).
		05 STUDENT-NAME-I PIC A(25).
FD OUTPUT.
	01 OUTPUT-STUDENT.
		05 STUDENT-ID-O PIC 9(5).
		05 STUDENT-NAME-O PIC A(25).
SD WORK.
	01 WORK-STUDENT.
		05 STUDENT-ID-W PIC 9(5).
		05 STUDENT-NAME-W PIC A(25).
PROCEDURE DIVISION.
	SORT WORK ON ASCENDING KEY STUDENT-ID-O
	USING INPUT GIVING OUTPUT.
	DISPLAY 'Sort Successful'.
STOP RUN.

JCL to execute the above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
//IN DD DSN=INPUT-FILE-NAME,DISP=SHR
//OUT DD DSN=OUTPUT-FILE-NAME,DISP=SHR
//WRK DD DSN=&&TEMP

When you compile and execute the above program, it produces the folloearng result:

Sort Successful

Merge Verb

Two or more identically sequenced files are combined uperform Merge statement. Files used in the merge process:

  • Inplace Files : Inplace-1, Inplace-2

  • Work File

  • Outplace File

Syntax

Folloearng is the syntax to merge 2 or more files:

MERGE work-file ON ASCENDING KEY rec-key1
              [ON DESCENDING KEY rec-key2]
USING inplace-1, inplace-2 GIVING out generally thereplace-file.

Merge performs the folloearng operations:

  • Opens the work-file in the I-O mode, inplace-files in the INPUT mode and out generally thereplace-file in the OUTPUT mode.

  • Transfers the records present in the inplace-files to the work-file.

  • Sorts the SORT-FILE in ascending/descending sequence simply simply by rec-key.

  • Transfers the sorted records from the work-file to the out generally thereplace-file.

  • Closes the inplace-file and the out generally thereplace-file and depermites the work-file.

Example

In the folloearng example, INPUT1 and INPUT2 are inplace the files which are to become merged in ascending order:

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
	SELECT INPUT1 ASSIGN TO IN1.
	SELECT INPUT2 ASSIGN TO IN2.
	SELECT OUTPUT ASSIGN TO OUT.
	SELECT WORK ASSIGN TO WRK.
DATA DIVISION.
FILE SECTION.
FD INPUT1.
	01 INPUT1-STUDENT.
		05 STUDENT-ID-I1 PIC 9(5).
		05 STUDENT-NAME-I1 PIC A(25).
FD INPUT2.
	01 INPUT2-STUDENT.
		05 STUDENT-ID-I2 PIC 9(5).
		05 STUDENT-NAME-I2 PIC A(25).
FD OUTPUT.
	01 OUTPUT-STUDENT.
		05 STUDENT-ID-O PIC 9(5).
		05 STUDENT-NAME-O PIC A(25).
SD WORK.
	01 WORK-STUDENT.
		05 STUDENT-ID-W PIC 9(5).
		05 STUDENT-NAME-W PIC A(25).
PROCEDURE DIVISION.
	MERGE WORK ON ASCENDING KEY STUDENT-ID-O
	USING INPUT1, INPUT2 GIVING OUTPUT.
	DISPLAY 'Merge Successful'.
STOP RUN.

JCL to execute the above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP1 EXEC PGM=HELLO
//IN1 DD DSN=INPUT1-FILE-NAME,DISP=SHR
//IN2 DD DSN=INPUT2-FILE-NAME,DISP=SHR
//OUT DD DSN=OUTPUT-FILE-NAME,DISP=SHR
//WRK DD DSN=&&TEMP

When you compile and execute the above program, it produces the folloearng result:

Merge Successful

COBOL – Database Interface

As of now, we have learnt the use of files in COBOL. Now, we will talk about generally there how a COBOL program interacts with DB2. It involves the folloearng terms:

  • Embecomedded SQL

  • DB2 Application Programming

  • Host Variables

  • SQLCA

  • SQL Queries

  • Cursors

Embecomedded SQL

Embecomedded SQL statements are used in COBOL programs to perform standard SQL operations. Embecomedded SQL statements are preprocessed simply simply by SQL processor becomefore the application program is compiimmediateed. COBOL is known as the Host Language. COBOL-DB2 applications are those applications thead wear include both COBOL and DB2.

Embecomedded SQL statements work like normal SQL statements with a couple of minimal modifys. For example, thead wear out generally thereplace of a query is immediateed to a pcrimsonegreatd set of variables which are refercrimson as Host Variables. An additional INTO clause is placed in the SELECT statement.

DB2 Application Programming

Folloearng are rules to become followed while coding a COBOL-DB2 program:

  • All the SQL statements must become delimited becometween EXEC SQL and END-EXEC.

  • SQL statements must become coded in Area B.

  • All the tables thead wear are used in a program must become declacrimson in the Worruler-Storage Section. This is done simply simply by uperform the INCLUDE statement.

  • All SQL statements other than INCLUDE and DECLARE TABLE must appear in the Procedure Division.

Host Variables

Host variables are used for receiving data from a table or inserting data in a table. Host variables must become declacrimson for all values thead wear are to become compenableeed becometween the program and the DB2. They are declacrimson in the Worruler-Storage Section.

Host variables cannot become group items, but they may become grouped sign up fortly in host structure. They cannot become Renamed or Redegreatd. Uperform host variables with SQL statements, prefix all of them with a colon (:).

Syntax

Folloearng is the syntax to declare host variables and include tables in Worruler-Storage section:

DATA DIVISION.
WORKING-STORAGE SECTION.
	EXEC SQL
	INCLUDE table-name
	END-EXEC.
	EXEC SQL BEGIN DECLARE SECTION
	END-EXEC.
		01 STUDENT-REC.
			05 STUDENT-ID PIC 9(4).
			05 STUDENT-NAME PIC X(25).
			05 STUDENT-ADDRESS X(50).
	EXEC SQL END DECLARE SECTION
	END-EXEC.

SQLCA

SQLCA is a SQL communication area through which DB2 compenableees the feedback of SQL execution to the program. It tells the program whether an execution was successful or not. There are a numbecomer of pcrimsonegreatd variables below SQLCA like SQLCODE which contains the error code. The value '000' in SQLCODE states a successful execution.

Syntax

Folloearng is the syntax to declare an SQLCA in the Worruler-Storage section:

DATA DIVISION.
WORKING-STORAGE SECTION.
	EXEC SQL
	INCLUDE SQLCA
	END-EXEC.

SQL Queries

Lets assume we have one table named as 'Student' thead wear contains Student-Id, Student-Name, and Student-Adgown.

The STUDENT table contains the folloearng data:

Student Id		Student Name		Student Adgown
1001 			Mohtashim M.		Hyderabad
1002			Nishant Malik		Delhi
1003 			Amitabh Bachan		Mumbai
1004			Chulbul Pandey		Lucknow

The folloearng example shows the usage of SELECT query in a COBOL program:

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
	EXEC SQL
	INCLUDE SQLCA
	END-EXEC.
	EXEC SQL
	INCLUDE STUDENT
	END-EXEC.
	EXEC SQL BEGIN DECLARE SECTION
	END-EXEC.
		01 WS-STUDENT-REC.
			05 WS-STUDENT-ID PIC 9(4).
			05 WS-STUDENT-NAME PIC X(25).
			05 WS-STUDENT-ADDRESS X(50).
	EXEC SQL END DECLARE SECTION
	END-EXEC.
PROCEDURE DIVISION.
	EXEC SQL
		SELECT STUDENT-ID, STUDENT-NAME, STUDENT-ADDRESS
		INTO :WS-STUDENT-ID, :WS-STUDENT-NAME, WS-STUDENT-ADDRESS FROM STUDENT
		WHERE STUDENT-ID=1004
	END-EXEC.
	IF SQLCODE=0 
		DISPLAY WS-STUDENT-RECORD
	ELSE DISPLAY 'Error'
	END-IF.
STOP RUN.

JCL to execute the above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP001  EXEC PGM=IKJEFT01
//STEPLIB  DD DSN=MYDATA.URMI.DBRMLIB,DISP=SHR
//SYSPRINT DD SYSOUT=*
//SYSUDUMP DD SYSOUT=*
//SYSOUT   DD SYSOUT=*
//SYSTSIN  DD *
    DSN SYSTEM(SSID)
    RUN PROGRAM(HELLO) PLAN(PLANNAME) -
    END
/*

When you compile and execute the above program, it produces the folloearng result:

1004 Chulbul Pandey		Lucknow

The folloearng example shows the usage of INSERT query in a COBOL program:

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
	EXEC SQL
	INCLUDE SQLCA
	END-EXEC.
	EXEC SQL
	INCLUDE STUDENT
	END-EXEC.
	EXEC SQL BEGIN DECLARE SECTION
	END-EXEC.
		01 WS-STUDENT-REC.
			05 WS-STUDENT-ID PIC 9(4).
			05 WS-STUDENT-NAME PIC X(25).
			05 WS-STUDENT-ADDRESS X(50).
	EXEC SQL END DECLARE SECTION
	END-EXEC.
PROCEDURE DIVISION.
	MOVE 1005 TO WS-STUDENT-ID.
	MOVE 'TutorialsPoint' TO WS-STUDENT-NAME.
	MOVE 'Hyderabad' TO WS-STUDENT-ADDRESS.
	EXEC SQL
		INSERT INTO STUDENT(STUDENT-ID, STUDENT-NAME, STUDENT-ADDRESS)
		VALUES (:WS-STUDENT-ID, :WS-STUDENT-NAME, WS-STUDENT-ADDRESS)
	END-EXEC.
	IF SQLCODE=0 
		DISPLAY 'Record Inserted Successcompenableey'
		DISPLAY WS-STUDENT-REC
	ELSE DISPLAY 'Error'
	END-IF.
STOP RUN.

JCL to execute the above COBOL program.

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP001  EXEC PGM=IKJEFT01
//STEPLIB  DD DSN=MYDATA.URMI.DBRMLIB,DISP=SHR
//SYSPRINT DD SYSOUT=*
//SYSUDUMP DD SYSOUT=*
//SYSOUT   DD SYSOUT=*
//SYSTSIN  DD *
    DSN SYSTEM(SSID)
    RUN PROGRAM(HELLO) PLAN(PLANNAME) -
    END
/*

When you compile and execute the above program, it produces the folloearng result:

Record Inserted Successcompenableey
1005 TutorialsPoint		Hyderabad

The folloearng example shows the usage of UPDATE query in a COBOL program:

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
	EXEC SQL
	INCLUDE SQLCA
	END-EXEC.
	EXEC SQL
	INCLUDE STUDENT
	END-EXEC.
	EXEC SQL BEGIN DECLARE SECTION
	END-EXEC.
		01 WS-STUDENT-REC.
			05 WS-STUDENT-ID PIC 9(4).
			05 WS-STUDENT-NAME PIC X(25).
			05 WS-STUDENT-ADDRESS X(50).
	EXEC SQL END DECLARE SECTION
	END-EXEC.
PROCEDURE DIVISION.
	MOVE 'Bangalore' TO WS-STUDENT-ADDRESS.
	EXEC SQL
		UPDATE STUDENT SET STUDENT-ADDRESS=:WS-STUDENT-ADDRESS
		WHERE STUDENT-ID=1003
	END-EXEC.
	IF SQLCODE=0 
		DISPLAY 'Record Updated Successcompenableey'
	ELSE DISPLAY 'Error'
	END-IF.
STOP RUN.

JCL to execute the above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP001  EXEC PGM=IKJEFT01
//STEPLIB  DD DSN=MYDATA.URMI.DBRMLIB,DISP=SHR
//SYSPRINT DD SYSOUT=*
//SYSUDUMP DD SYSOUT=*
//SYSOUT   DD SYSOUT=*
//SYSTSIN  DD *
    DSN SYSTEM(SSID)
    RUN PROGRAM(HELLO) PLAN(PLANNAME) -
    END
/*

When you compile and execute the above program, it produces the folloearng result:

Record Updated Successcompenableey

The folloearng example shows the usage of DELETE query in a COBOL program:

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
	EXEC SQL
	INCLUDE SQLCA
	END-EXEC.
	EXEC SQL
	INCLUDE STUDENT
	END-EXEC.
	EXEC SQL BEGIN DECLARE SECTION
	END-EXEC.
		01 WS-STUDENT-REC.
			05 WS-STUDENT-ID PIC 9(4).
			05 WS-STUDENT-NAME PIC X(25).
			05 WS-STUDENT-ADDRESS X(50).
	EXEC SQL END DECLARE SECTION
	END-EXEC.
PROCEDURE DIVISION.
	MOVE 1005 TO WS-STUDENT-ID.
	EXEC SQL
		DELETE FROM STUDENT
		WHERE STUDENT-ID=:WS-STUDENT-ID
	END-EXEC.
	IF SQLCODE=0 
		DISPLAY 'Record Depermited Successcompenableey'
	ELSE DISPLAY 'Error'
	END-IF.
STOP RUN.

JCL to execute the above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP001  EXEC PGM=IKJEFT01
//STEPLIB  DD DSN=MYDATA.URMI.DBRMLIB,DISP=SHR
//SYSPRINT DD SYSOUT=*
//SYSUDUMP DD SYSOUT=*
//SYSOUT   DD SYSOUT=*
//SYSTSIN  DD *
    DSN SYSTEM(SSID)
    RUN PROGRAM(HELLO) PLAN(PLANNAME) -
    END
/*

When you compile and execute the above program, it produces the folloearng result:

Record Depermited Successcompenableey

Cursors

Cursors are used to handle multiple line selections at a time. They are data structures thead wear hold all the results of a query. They can become degreatd in the Worruler-Storage Section or the Procedure Division. Folloearng are the operations associated with Cursor:

  • Declare

  • Open

  • Close

  • Fetch

Declare Cursor

Cursor declaration can become done in the Worruler-Storage Section or the Procedure Division. The 1st statement is the DECLARE statement which is a non-executable statement.

EXEC SQL
	DECLARE STUDCUR CURSOR FOR
	SELECT STUDENT-ID, STUDENT-NAME, STUDENT-ADDRESS FROM STUDENT
	WHERE STUDENT-ID >:WS-STUDENT-ID
END-EXEC.

Open

Before uperform a cursor, Open statement must become performed. The Open statement prepares the SELECT for execution.

EXEC SQL
	OPEN STUDCUR
END-EXEC.

Close

Close statement relrelayves all the memory occupied simply simply by the cursor. It is mandatory to near a cursor becomefore ending a program.

EXEC SQL
	CLOSE STUDCUR
END-EXEC.

Fetch

Fetch statement identifies the cursor and places the value in the INTO clause. A Fetch statement is coded in loop as we get one line at a time.

EXEC SQL
	FETCH STUDCUR
	INTO :WS-STUDENT-ID, :WS-STUDENT-NAME, WS-STUDENT-ADDRESS
END-EXEC.

The folloearng example shows the usage of cursor to fetch all the records from the STUDENT table:

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
WORKING-STORAGE SECTION.
	EXEC SQL
	INCLUDE SQLCA
	END-EXEC.
	EXEC SQL
	INCLUDE STUDENT
	END-EXEC.
	EXEC SQL BEGIN DECLARE SECTION
	END-EXEC.
		01 WS-STUDENT-REC.
			05 WS-STUDENT-ID PIC 9(4).
			05 WS-STUDENT-NAME PIC X(25).
			05 WS-STUDENT-ADDRESS X(50).
	EXEC SQL END DECLARE SECTION
	END-EXEC.
	EXEC SQL
		DECLARE STUDCUR CURSOR FOR
		SELECT STUDENT-ID, STUDENT-NAME, STUDENT-ADDRESS FROM STUDENT
		WHERE STUDENT-ID >:WS-STUDENT-ID
	END-EXEC.
PROCEDURE DIVISION.
	MOVE 1001 TO WS-STUDENT-ID.
	PERFORM UNTIL SQLCODE = 100
	EXEC SQL
		FETCH STUDCUR
		INTO :WS-STUDENT-ID, :WS-STUDENT-NAME, WS-STUDENT-ADDRESS
	END-EXEC
	DISPLAY WS-STUDENT-REC
	END-PERFORM	
STOP RUN.

JCL to execute the above COBOL program:

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS=A,MSGCLASS=C
//STEP001  EXEC PGM=IKJEFT01
//STEPLIB  DD DSN=MYDATA.URMI.DBRMLIB,DISP=SHR
//SYSPRINT DD SYSOUT=*
//SYSUDUMP DD SYSOUT=*
//SYSOUT   DD SYSOUT=*
//SYSTSIN  DD *
    DSN SYSTEM(SSID)
    RUN PROGRAM(HELLO) PLAN(PLANNAME) -
    END
/*

When you compile and execute the above program, it produces the folloearng result:

1001 Mohtashim M.		Hyderabad
1002 Nishant Malik		Delhi
1003 Amitabh Bachan		Mumbai
1004 Chulbul Pandey		Lucknow
SHARE
Previous articleJOGL
Next articleJUnit

NO COMMENTS

LEAVE A REPLY