Prashant - COBOL 400

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 44

Objective: To provide information that an

application programmer needs to write, compile,


debug and run COBOL/400 programs on IBM
Application System/400 (AS/400) system.

Features available with


COBOL/400

TRANSACTION I/O: you can send or receive records


from a work station.

COPY: You can use externally described files.

DATABASE I/O: You can use standard COBOL


Environment and Data Division entries to specify file
identification, field definitions and data structures.
Clauses have been added to the READ, WRITE, REWRITE,
DELETE and START verbs to support the AS/400 database.

LIKE clause: you can define the characteristics of a data


name by copying them from a previously-defined data
name.

Steps or phases to build your


COBOL/400 program
Entering your COBOL Program

(SEU) provides a special display to help you enter an accurate COBOL


source program into the system. The SEU also provides a COBOL syntax
checker that checks each line for errors as you enter or change them.

Compiling Your COBOL Program

Create COBOL Program using CRTCBLPGM command


Using the PROCESS Statement to Specify Compiler Options
Understanding the compiler listing output and resolving the errors

Debugging Your COBOL Program

Using Start interactive source debugger (STRISDB) OR Start Debug


(STRDBG) command and setting up breakpoints

Running Your COBOL Program

Using a Control Language (CL) CALL command


Using the COBOL CALL statement
Using a menu-driven application program
Calling from the Command Line

Structure of a COBOL/400 Program


IDENTIFICATION DIVISION.
PROGRAM-ID.
XYZ.
AUTHOR.
ABC.
DATE-WRITTEN.
24 September, 2008.
DATE-COMPILED.
25 September, 2008.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. IBM-AS400.
OBJECT-COMPUTER. IBM-AS400.
SPECIAL-NAMES.
LOCAL DATA IS LDA-AREA.
Contd.

Structure of a COBOL/400
Program
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT FILE-NAME
ASSIGN
TO DATABASE-DB-FILE NAME
ORGANIZATION
IS INDEXED
ACCESS
IS DYNAMIC
RECORD KEY
IS EXTERNALLY-DESCRIBED-KEY
FILE STATUS
IS FILE-STATUS.

Assign
The following device files can be used with the
Assign Clause:

DATABASE
WORKSTATION

For Physical and Logical files


For Display file

System Indicator - SI

SELECT DISPFILE
ASSIGN TO WORKSTATION-DSPFIL-SI
INDARA This file level keyword is specified in the
Display File to assign a separate Indicator Area.
If specified, the Input and the Output Buffer of the
need
value.

indicator is handled by the system. Theres no


to clear the buffer before using the Indicator

Organization
Type of logical structure (organization) of the file:
SEQUENTIAL the records are accessed sequentially,
the order in which they are entered.

INDEXED the position of each logical record in the


file is determined by the key sequence access path
created with the file and maintained by system.

RELATIVE - the position of each record in the file is


determined by its relative record number within the
arrival sequence path.

TRANSACTION signifies interaction between a COBOL


program and a workstation user.

Access Mode
Following are the types of Access Modes:
Sequential Allows reading and writing of a file in a
serial manner - can be specified for sequential, indexed
or relative Files.
Random Allows reading and writing records in a
programmer-specified manner
Dynamic Records may be processed sequentially or
randomly

Advantages and
Disadvantages

Sequential Files

Advantages:
Simple organization, Recovers space from
deleted records.
Disadvantages:
Slow, Complicated to change (insert, delete,
amend)

Advantages and
Disadvantages Contd.

Relative Files

Advantages:
Fastest Direct Access organization, Very
little storage overhead.
Disadvantages:
Can not recover space from deleted records,
Only a single numeric key allowed.

Advantages and
Disadvantages Contd.

Indexed Files

Advantages:
Can use multiple, alphanumeric keys, Can
be read sequentially on any of its keys.
Disadvantages:
Slow when adding or deleting records.

Key

Record Key The file key is specified.

Relative Key The RELATIVE KEY clause specifies the relative record
number for a specific record in a subfile. The field specified as the
relative key needs to declared in the Working Storage Section.
RELATIVE KEY IS WB-RRN

WORKING-STORAGE-SECTION.
01 WB-RRN
PIC 9(5) Value 0

File Status Clause

This clause is used to monitor the processing of each I/O


request for the file.
When specified system moves a value (which indicates
the status of execution
of the statement) into the data item after each input output
that implicitly or
explicitly refers to the file.

Used to monitor for errors and exceptional conditions.


FILE STATUS IS WB-FILE-STATUS

WORKING-STORAGE-SECTION.
01 WB-FILE-STATUS

PIC X(2).

File Status Examples

00 Successful Completion
22 Duplicate Key
23 Record Not Found
9D Record is Locked

Structure of a COBOL/400
Program Contd.
DATA DIVISION.
FILE SECTION.
FD DB-FILE
LABEL RECORDS ARE STANDARD.
01 DB-REC.
COPY DDS-ALL-FORMATS
WORKING-STORAGE SECTION.
01 FLD-REC.
05 FLD1
PIC S9(005)
05 FLD2
PIC X(005)

OF DB FILE NAME.

VALUE 0.
VALUE Y.

LINKAGE SECTION.
01 LINKAGE-AREA.
05 LS-PARM1
05 LA-PARM2

PIC X(002).
PIC X(002).

Label Records
Following are the types of Label Records:

Standard

Omitted
Used for Display Files
Standard If specified, the AS/400 system would consider
the first record as a Label Record and the file would be
hence read from the second record.
Omitted If specified, the AS/400 system would not
consider the first record as a Label Record.

Working Storage Section

Level 66 - Identifies items that must contain a


RENAMES clause
Level 77 - Identifies data item description
entries independent WorkingStorage or Linkage Section items
that are not subdivisions of other
items, and are not subdivided themselves.
Level 88 - Identifies any condition-name entry that
is
associated with a particular value of a
conditional variable.

Declaring Indicators
WORKING-STORAGE SECTION.
01 INDIC-AREA.
05 INDIC-TABLE OCCURS 99
88 IND-OFF
88 IND-ON
01 WS-INDICATORS.
05 IND-HELP
05 IND-KEY2
05 IND-EXIT
05 IND-CANCEL
05 IND-SFLNXTCHG
05 IND-SFLDSP
05 IND-SFLDSPCTL
05 IND-SFLCLEAR
05 IND-SFLEND

PIC
PIC
PIC
PIC
PIC
PIC
PIC
PIC
PIC

PIC 1
INDICATOR 1.
VALUE B"0".
VALUE B"1".

9(02)
9(02)
9(02)
9(02)
9(02)
9(02)
9(02)
9(02)
9(02)

VALUE 01.
VALUE 02.
VALUE 03.
VALUE 12.
VALUE 30.
VALUE 31.
VALUE 32.
VALUE 33.
VALUE 34.

Screen
CF12(12 'Cancel / Previous Screen')
CF03(03 'Exit Screen')
R IUW0034S05
N32
N31
31
90

SFLDSP
SFLDSPCTL
SFLCLR
SFLEND(*MORE)

Redefines Clause
Redefines - The REDEFINES clause allows you to use
different
data description entries to describe the same
computer storage area.
For example:
05 A
PIC X(6).
05 B REDEFINES A.
10 B-1
PIC X(2).
10 B-2
PIC 9(4).
05 C
PIC 99V99.

Rename Clause
Rename The RENAME clause allows
alternative grouping of Data
Items.
For Example:
01 RECORD-1.
05 FLD-1
05 FLD-2
05 FLD-3

PIC X(10).
PIC X(05).
PIC X(05).

66 FLD-4 RENAMES FLD-1 THROUGH FLD2

Linkage Section

This section is used to specify the linkage parameters.


LINKAGE SECTION.
01 LINKAGE-AREA.
05 LS-PARM1
05 LA-PARM2

PIC X(002).
PIC X(002).

Calling Program CALL PGM1 USING WS-PARM1 WS-PARM2


Called Program It will contain the Linkage section and the
Procedure Division would be written as:
PROCEDURE DIVISION USING LINKAGE-AREA.

Structure of a COBOL/400
Program Contd.
PROCEDURE DIVISION
USING LINKAGE-AREA.
0000-MAIN-PARA.
PERFORM 0100-INITIALIZE-PROGRAM THRU 0100-EXIT.
0100-INITIALIZE-PROGRAM.
ACCEPT LOCAL-DATA-AREA FROM LDA-AREA.
OPEN INPUT FILE1
I-O
FILE2 .
0100-EXIT.
EXIT.

Controls

Next Sentence Specified to Transfer Control to the


next executable line.
Continue Specified to indicate that no executable
instruction is
present.
Go Back Specified to Transfer Control from the Called
Program
to the Calling Program.
Stop Run Specified to terminate the execution of a
Program.
The execution of the run-unit is terminated.

Controls Contd..

Exit program The EXIT PROGRAM


statement specifies the
end of a called program
and returns control to
the calling program.

Go To - The GO TO statement transfers control from


one part of the Procedure Division to another

Controls Contd..

Perform - The PERFORM statement transfers


control explicitly to one or more
procedures and implicitly returns
control to the next executable
statement after execution of the
specified procedure(s) or
imperative statements is completed.

Types of PERFORM

An out-of-line PERFORM statement:


PERFORM 1000-OPEN-PARA.

1000-OPEN-PARA.
*===============
OPEN I-O
DISPFILE
OUTPUT REPORTFILE.

An in-line PERFORM statement:


PERFORM
MOVE A
TO FLD-A
MOVE B
TO FLD-B
END-PERFORM

ACCOUNTS

PERFORM Statement
Formats

There are four PERFORM statement formats:

Basic PERFORM
PERFORM 1000-OPEN-PARA.

TIMES phrase PERFORM


PERFORM 1000-OPEN-PARA 5 TIMES.

UNTIL phrase PERFORM


PERFORM 1000-OPEN-PARA UNTIL WS-EOF NOT = Y

VARYING phrase PERFORM


PERFORM 1000-OPEN-PARA VARYING WS-I FROM 1 BY 1
UNTIL WS-EOF = Y

Handling Indicators
Change the Value of an Indicator:
MOVE B"0"

TO INDIC-TABLE(IND-SFLEND)

Check the Value of an Indicator:


IF IND-ON(IND-EXIT)
MOVE 99
END-IF.

TO WS-RECNO

Procedure Division
Keywords

String
STRING WS-RESERVE-TYPE(1) ")" DELIMITED BY
SPACES INTO WS-RESERVE-TYP-C(1)
END-STRING

Unstring
UNSTRING WS-SUB-GRP(WS-F) DELIMITED BY ','
INTO WS-VALID-RESV-CODES(WS-F, 1)
WS-VALID-RESV-CODES(WS-F, 2)
END-UNSTRING
Add
Compute
Move

Evaluate
Evaluate
EVALUATE WS-IND1
WHEN "01"
ADD 1
TO WS-RESV1-CNT
ADD WS-CRS-RES-AMT(WS-H)
TO WS-RESV1-TOTAL-AMT
WHEN "02"
ADD 1
TO WS-RESV2-CNT
ADD WS-CRS-RES-AMT(WS-H)
TO WS-RESV2-TOTAL-AMT
END-EVALUATE

File Handling in Cobol

Open a File:

Start a File:
Move values to the key
START CLAIMRES KEY NOT < CRS-KEY
INVALID KEY MOVE "Y"
TO
WS-EOF-CLAIMRES
END-START.

Read a File
Move values to the key
READ CLAIMRES NEXT
AT END MOVE "Y"
TO WS-EOF-CLAIMRES
NOT AT END MOVE "N"
TO WS-EOFCLAIMRES
END-READ.

File Handling Contd

Write a Record in the File


Move Values to the fields
WRITE ITMCLRS06P-REC.

Re-write a Record in the File


Move Values to the fields
RE-WRITE ITMCLRS06P-REC.

Delete a File
Move Values to the fields
DELETE FILE-NAME

Reports
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT PRINTFILE
ASSIGN

TO PRINTER-QPRINTS.

DATA DIVISION.
FILE SECTION.
FD PRINTFILE
LABEL RECORDS ARE OMITTED
LINAGE IS 66 LINES
LINES AT TOP 1.
01 PRINT-REC
PIC X(198).

Reports Contd..
WORKING-STORAGE SECTION.
01 RPT-MAIN-HEADING-01.
05 FILLER
PIC X(03) VALUE SPACES.
05 RPT-HDG
PIC X(13) VALUE SPACES.
05 FILLER
PIC X(182) VALUE SPACES.
PROCEDURE DIVISION.
MOVE "Overall Total"
TO RPT-HDG
WRITE PRINT-REC FROM RPT-MAIN-HEADING-03.

Reports Contd..
To Write in the Next Page:
IF LINAGE-COUNTER OF PRINTFILE > 62
ADD 1
TO WS-PAGE-NO
MOVE "Overall Total"
TO RPT-HDG
WRITE PRINT-REC FROM RPT-MAIN-HEADING-01 AFTER
PAGE
END-IF

Subfiles Screen Design


R IUW0034S04
SFL
30
SFLNXTCHG
DFSELOPT
1A B 6
DFCODE
2A O 6 7
DFDESC
40A O 6 20
R IUW0034S05
SFLCTL(IUW0034S04)
SFLSIZ(0100)
SFLPAG(0010)
31
SFLDSP
32
SFLDSPCTL
33
SFLCLR
34
SFLEND

Steps For Handling


Subfiles

Initialize the Function Keys


Clear The Subfile
Load the Subfile
Display the Subfile

Initialize the Function Keys


MOVE B"0"

TO INDIC-TABLE(IND-SFLEND)
INDIC-TABLE(IND-SFLDSP)
INDIC-TABLE(IND-SFLDSPCTL)
INDIC-TABLE(IND-SFLNXTCHG)

Clear The Subfile


MOVE B"1"

TO INDIC-TABLE(IND-SFLCLEAR)

Load the Subfile


READ ACCOUNTS NEXT RECORD
PERFORM 3300-READ-ACCNTS UNTIL WS-EOF-ACCNTS = "Y
3300-READ-ACCNTS.
*===================
ADD 1
TO WS-COUNT
READ ACCOUNTS NEXT RECORD
AT END MOVE "Y" TO WS-EOF-ACCNTS
END-READ
MOVE AC-MAIN
TO ACMAIN OF SUBFIL-O
MOVE AC-SUB
TO ACSUB OF SUBFIL-O
MOVE AC-GST-REGD
TO GSTRGD OF SUBFIL-O
MOVE AC-GST-REG-NO
TO GSTRGDNO OF SUBFIL-O
MOVE AC-GST-BILL
TO GSTBILL OF SUBFIL-O
WRITE SUBFILE DISP-REC FORMAT IS "SUBFIL"
INDICATORS ARE INDIC-TABLE.

Display the Subfile


MOVE B"1"

TO INDIC-TABLE(IND-SFLDSP)
INDIC-TABLE(IND-SFLDSPCTL)
INDIC-TABLE(IND-SFLEND)

WRITE DISP-REC FORMAT IS "SUBFILCTL"


INDICATORS ARE INDIC-TABLE
END-WRITE
READ DISPFILE FORMAT IS "SUBFILCTL"
INDICATORS ARE INDIC-TABLE
END-READ

Arrays
Declaring:
05 WS-VALID-RESV-CDES OCCURS 5 TIMES INDEXED BY WS-SUB2.
10 WS-VALID-RESV-CODES PIC X(04).
Searching:
PERFORM VARYING WS-SUB1 FROM 1 BY 1 UNTIL WS-SUB1 >
OR WS-RESV-FOUND = "Y"
SET WS-SUB2
TO 1
SEARCH WS-VALID-RESV-CDES
WHEN WS-VALID-RESV-CDES(WS-SUB1, WS-SUB2) =
WS-CRS-RES-CODE(WS-H)
SET WS-IND1
TO WS-SUB1
SET WS-IND2
TO WS-SUB2
MOVE "Y"
TO WS-RESV-FOUND
PERFORM 2065-ADD-TO-RESV-PARA THRU
2065-ADD-TO-RESV-PARA-EXIT
END-SEARCH
END-PERFORM

You might also like