Mainframe - Key Topics
Mainframe - Key Topics
Description:
Suitable when dealing with small datasets or unsorted data, such as searching for a
specific transaction type in a customer's daily transaction list.
Example:
cobol
Copy code
WORKING-STORAGE SECTION.
01 WS-TABLE.
05 WS-ACC-NUMBER OCCURS 10 TIMES INDEXED BY IDX.
10 WS-ACC VALUE SPACES.
PROCEDURE DIVISION.
MOVE '1234567890' TO WS-SEARCH-NUMBER.
PERFORM VARYING IDX FROM 1 BY 1 UNTIL IDX > 10
SEARCH WS-ACC-NUMBER
WHEN WS-ACC (IDX) = WS-SEARCH-NUMBER
MOVE 'Y' TO WS-FOUND
DISPLAY "Account Found at index: " IDX
END-SEARCH
END-PERFORM.
IF WS-FOUND = 'N'
DISPLAY "Account Not Found."
END-IF.
Description:
SEARCH ALL is used for binary search in a sorted array.
The table must be sorted in ascending or descending order before the search.
Faster than SEARCH, especially for large datasets.
Commonly used in scenarios involving large datasets that require high-speed searches,
such as customer ID searches, account lookups, or locating a specific transaction in a
sorted ledger.
Example:
cobol
Copy code
WORKING-STORAGE SECTION.
01 WS-TABLE.
05 WS-ACC-NUMBER OCCURS 100 TIMES ASCENDING KEY IS WS-ACC-ID INDEXED BY
IDX.
10 WS-ACC-ID PIC X(10).
10 WS-ACC-BALANCE PIC 9(8)V99.
PROCEDURE DIVISION.
MOVE '1234567890' TO WS-SEARCH-NUMBER.
SORT WS-TABLE ON ASCENDING KEY WS-ACC-ID.
IF WS-FOUND = 'N'
DISPLAY "Account Not Found."
ELSE
DISPLAY "Account Balance: " WS-ACC-BALANCE (IDX)
END-IF.
Comparison
This example demonstrates a SEARCH operation to find a specific transaction type in a list of
transactions.
cobol
Copy code
WORKING-STORAGE SECTION.
* Define a table to store transaction types.
01 WS-TRANSACTION-TABLE.
05 WS-TRANSACTION OCCURS 5 TIMES INDEXED BY IDX.
10 WS-TRANS-TYPE PIC X(10). * Transaction type (e.g., DEPOSIT,
WITHDRAW).
PROCEDURE DIVISION.
* Initialize the transaction types in the table.
MOVE 'DEPOSIT' TO WS-TRANS-TYPE (1).
MOVE 'WITHDRAW' TO WS-TRANS-TYPE (2).
MOVE 'TRANSFER' TO WS-TRANS-TYPE (3).
MOVE 'LOAN' TO WS-TRANS-TYPE (4).
MOVE 'PAYMENT' TO WS-TRANS-TYPE (5).
This example demonstrates a SEARCH ALL operation to find a customer ID in a sorted table of
customer records.
cobol
Copy code
WORKING-STORAGE SECTION.
* Define a table to store customer records.
01 WS-CUSTOMER-TABLE.
05 WS-CUSTOMERS OCCURS 10 TIMES ASCENDING KEY IS WS-CUST-ID INDEXED BY IDX.
10 WS-CUST-ID PIC X(10). * Customer ID (10 characters).
10 WS-CUST-NAME PIC X(20). * Customer Name.
PROCEDURE DIVISION.
* Initialize the customer records in sorted order.
MOVE 'CUST001' TO WS-CUST-ID (1). MOVE 'Alice' TO WS-CUST-NAME (1).
MOVE 'CUST002' TO WS-CUST-ID (2). MOVE 'Bob' TO WS-CUST-NAME (2).
MOVE 'CUST003' TO WS-CUST-ID (3). MOVE 'Charlie' TO WS-CUST-NAME (3).
MOVE 'CUST004' TO WS-CUST-ID (4). MOVE 'Diana' TO WS-CUST-NAME (4).
MOVE 'CUST005' TO WS-CUST-ID (5). MOVE 'Eve' TO WS-CUST-NAME (5).
IEBEDIT is used for editing, printing, and modifying datasets. It’s commonly used to modify or
update datasets based on specified conditions.
This example demonstrates using IEBEDIT to update an account number in a dataset based on
certain conditions (e.g., update account number for a specific customer).
Code Example:
java
Copy code
//EDITEXAMPLE JOB (ACCT),'IEBEDIT JOB',CLASS=A,MSGCLASS=A
//STEP1 EXEC PGM=IEBEDIT
//SYSPRINT DD SYSOUT=A
//SYSIN DD *
EDIT FILE(INPUT)
OUTDD(OUTPUT)
FIND '1234567890' * Find the record with account number
'1234567890'.
CHANGE '1234567890' TO '9876543210' * Change '1234567890' to '9876543210'.
END
//INPUT DD DSN=ACCOUNT.DATA,DISP=SHR * Input dataset containing account
information.
//OUTPUT DD DSN=UPDATED.ACCOUNT.DATA,DISP=MOD * Output dataset with updated
account numbers.
Explanation:
ICETOOL is a powerful utility for processing and manipulating datasets in various ways, such
as concatenation, sorting, and reporting.
This example demonstrates how ICETOOL can be used to sort multiple datasets and
concatenate them into one output dataset.
Code Example:
scss
Copy code
//SORTCONCAT JOB (SORT),'ICETOOL JOB',CLASS=A,MSGCLASS=A
//STEP1 EXEC PGM=ICETOOL
//TOOLIN DD *
SORT FROM(IN1) TO(OUT)
USING(CTL1)
DISPLAY
COND(4,0)
//IN1 DD DSN=FIRST.DATASET,DISP=SHR * First input dataset.
//IN2 DD DSN=SECOND.DATASET,DISP=SHR * Second input dataset.
//CTL1 DD *
SORT FIELDS=(1,10,CH,A) * Sorting based on the first 10 characters in
ascending order.
//OUT DD DSN=CONCATENATED.DATASET,DISP=NEW * Output dataset after sorting
and concatenation.
//SYSOUT DD SYSOUT=A
//DISPLAY DD SYSOUT=A
Explanation:
This example demonstrates how IEBEDIT can be used to delete lines containing a specific
string in an input dataset.
Code Example:
css
Copy code
//EDITREMOVE JOB (REMOVE),'DELETE LINES',CLASS=A,MSGCLASS=A
//STEP1 EXEC PGM=IEBEDIT
//SYSPRINT DD SYSOUT=A
//SYSIN DD *
EDIT FILE(INPUT)
OUTDD(OUTPUT)
FIND 'DELETED' * Find all lines containing the word
'DELETED'.
REMOVE * Remove those lines from the dataset.
END
//INPUT DD DSN=DATASET.TO.EDIT,DISP=SHR * Input dataset.
//OUTPUT DD DSN=DATASET.AFTER.EDIT,DISP=MOD * Output dataset without
'DELETED' lines.
Explanation:
Use Case:
Useful in cleaning up data where certain entries (e.g., flagged as "DELETED") need to be
removed from large datasets, such as transaction logs or customer records.
2. Add a Prefix to Each Line Using IEBEDIT
This example shows how IEBEDIT can add a prefix to every line in the dataset.
Code Example:
scss
Copy code
//EDITPREFIX JOB (PREFIX),'ADD PREFIX',CLASS=A,MSGCLASS=A
//STEP1 EXEC PGM=IEBEDIT
//SYSPRINT DD SYSOUT=A
//SYSIN DD *
EDIT FILE(INPUT)
OUTDD(OUTPUT)
ADD PREFIX 'PREFIX_' TO EACH LINE * Add 'PREFIX_' to the beginning of each
line.
END
//INPUT DD DSN=DATASET.TO.PREFIX,DISP=SHR * Input dataset.
//OUTPUT DD DSN=DATASET.AFTER.PREFIX,DISP=MOD * Output dataset with added
prefix.
Explanation:
ADD PREFIX 'PREFIX_' TO EACH LINE: Adds the string 'PREFIX_' to the beginning of each line in
the dataset.
Useful for adding tags or identifiers to each line of data, such as customer ID or transaction ID.
Use Case:
Useful in scenarios where you want to add specific labels or identifiers to each line of data, such
as marking records with a unique prefix or suffix for later identification.
This example shows how to use ICETOOL to sort data based on multiple fields, such as
customer name and balance.
Code Example:
scss
Copy code
//SORTMULTIFIELD JOB (SORT),'SORT DATA',CLASS=A,MSGCLASS=A
//STEP1 EXEC PGM=ICETOOL
//TOOLIN DD *
SORT FROM(IN1) TO(OUT)
USING(CTL1)
DISPLAY
//IN1 DD DSN=CUSTOMER.DATA,DISP=SHR * Input dataset with customer data
(Name and Balance).
//OUT DD DSN=SORTED.CUSTOMER.DATA,DISP=NEW * Output dataset after
sorting.
//CTL1 DD *
SORT FIELDS=(1,20,CH,A,21,8,PD,D) * First sort by customer name (first 20
characters), then by balance (next 8 digits).
//SYSOUT DD SYSOUT=A
Explanation:
Use Case:
Useful for processing large datasets like customer records, where sorting is required by multiple
fields (e.g., alphabetical sorting by name and numerical sorting by account balance).
This example demonstrates how ICETOOL can be used to concatenate multiple datasets into
one output dataset.
Code Example:
css
Copy code
//MERGEJOB JOB (MERGE),'MERGE DATASETS',CLASS=A,MSGCLASS=A
//STEP1 EXEC PGM=ICETOOL
//TOOLIN DD *
CONCATENATE FILES(IN1, IN2) TO(OUT) * Concatenate the two input datasets
into one output dataset.
DISPLAY
//IN1 DD DSN=FIRST.DATASET,DISP=SHR * First input dataset.
//IN2 DD DSN=SECOND.DATASET,DISP=SHR * Second input dataset.
//OUT DD DSN=CONCATENATED.DATASET,DISP=NEW * Output dataset containing
the merged records.
//SYSOUT DD SYSOUT=A
Explanation:
CONCATENATE FILES(IN1, IN2) TO(OUT): This command concatenates IN1 and IN2 into the
output dataset OUT.
After concatenation, the records from both datasets will be in the output dataset in the order
they were processed.
Use Case:
Useful when you need to combine multiple datasets (such as logs, transaction files, or customer
records) into a single dataset for further processing.
This example demonstrates how to generate a report by sorting and displaying specific data from
a dataset.
Code Example:
scss
Copy code
//REPORTGEN JOB (REPORT),'GENERATE REPORT',CLASS=A,MSGCLASS=A
//STEP1 EXEC PGM=ICETOOL
//TOOLIN DD *
REPORT FILE(INPUT) TO(OUTPUT) COUNT(3) * Generate a report of the first 3
records.
DISPLAY
//INPUT DD DSN=SALES.DATA,DISP=SHR * Input dataset with sales data.
//OUTPUT DD DSN=REPORT.DATA,DISP=NEW * Output dataset for the report.
//SYSOUT DD SYSOUT=A
Explanation:
REPORT FILE(INPUT) TO(OUTPUT) COUNT(3): This command generates a report that takes the
first 3 records from the INPUT dataset and writes them to the OUTPUT dataset.
DISPLAY: Outputs information to the system’s console about the report generation.
Use Case:
Useful when you need to create summaries or reports for a small number of records from a
larger dataset, such as generating weekly or monthly reports from transaction data.
ICETOOL - Sort and concatenate datasets - Sorting customer data by multiple fields
DB2 Abends –
Root Cause:
o This is a common DB2 abend indicating that the DB2 subsystem is unavailable or there is
an issue with connecting to DB2 resources (such as DB2 database, tablespace, or buffer
pool).
o It typically happens when the DB2 system is not up or there is a failure in accessing the
required resource.
SQLCODE -904 Example:
vbnet
Copy code
SQLCODE = -904, ERROR: A DB2 resource is unavailable
Possible Causes:
o DB2 subsystem is down.
o Tablespace or index is unavailable.
o Insufficient system resources (e.g., memory or disk space).
Resolution:
o Check DB2 logs and subsystem status (db2start or db2stop commands).
o Ensure the necessary tablespaces or indexes are available.
o Verify that DB2 services are up and running properly.
o Reallocate resources (memory, disk, buffer pool) as needed.
Root Cause:
o This abend occurs when DB2 is unable to find the specified package or plan. This
typically happens if:
The package has not been bound to the DB2 subsystem.
The user does not have permission to access the package.
SQLCODE -805 Example:
lua
Copy code
SQLCODE = -805, ERROR: DB2 cannot find the package
Possible Causes:
o Missing or unbound DB2 package.
o Invalid or incomplete bind of the application program.
Resolution:
o Ensure the program is bound to the DB2 subsystem.
o Use the BIND command to bind the plan or package correctly.
o Check the DB2 catalog to ensure the required package is present and valid.
Root Cause:
o This error occurs when DB2 detects a deadlock (where two or more transactions are
stuck in a cyclic dependency) or when a transaction exceeds the timeout limit.
o In a deadlock scenario, DB2 resolves the issue by rolling back one of the transactions.
SQLCODE -911 Example:
vbnet
Copy code
SQLCODE = -911, ERROR: Deadlock or Timeout error
Possible Causes:
o Transactions are waiting on each other, causing a deadlock.
o Database operations are taking too long, causing a timeout.
Resolution:
o Investigate the transaction flow to find the root cause of the deadlock.
o Use the DB2 deadlock detection mechanism to identify conflicting transactions.
o Ensure that transactions are optimized for speed, and the lock contention is minimized.
Example of Deadlock Detection:
sql
Copy code
SELECT *
FROM SYSIBM.SYSTABLES
WHERE TABLE_NAME = 'MY_TABLE'
DB2 Timeout Parameters: Adjust the lock timeout settings in DB2 to resolve timeout-related
issues:
bash
Copy code
UPDATE DBM CFG USING LOCKTIMEOUT 60
Root Cause:
o This abend occurs when you try to insert or update a record that violates a unique
constraint or primary key.
o For example, inserting a duplicate value in a field that must be unique.
SQLCODE -803 Example:
sql
Copy code
SQLCODE = -803, ERROR: Unique constraint violation
Possible Causes:
o Duplicate values being inserted into columns with a unique constraint or primary key.
Resolution:
o Ensure that the data being inserted or updated does not violate unique constraints.
o Use a SELECT statement to check for duplicates before insertion.
Root Cause:
o This abend occurs when DB2 cannot find the host variable specified in the SQL query. It
may happen if the host variable is incorrectly defined or not passed correctly.
SQLCODE -805 Example:
makefile
Copy code
SQLCODE = -805, ERROR: Invalid host variable
Possible Causes:
o The host variable is either not declared, or the incorrect variable is being used in the
program.
Resolution:
o Ensure the host variable is declared properly and passed correctly to the DB2 statement.
o Verify that the host variable matches the data type defined in the DB2 table.
Root Cause:
o This error occurs when you attempt to insert or update a null value into a column that
does not allow null values.
SQLCODE -305 Example:
yaml
Copy code
SQLCODE = -305, ERROR: Null value in a column that doesn't allow NULL
Possible Causes:
o The column does not allow NULL values, and a NULL value is being inserted or updated.
Resolution:
o Ensure that NULL values are not inserted into columns that do not allow them.
o Modify the column definition to allow NULL values if appropriate.
Root Cause:
o This error occurs when a SELECT query does not return any rows. It is not considered a
fatal error but indicates that the requested data does not exist.
SQLCODE -100 Example:
yaml
Copy code
SQLCODE = -100, ERROR: No data found
Possible Causes:
o The query was executed with search conditions that do not match any rows.
Resolution:
o Use a fetch loop to handle the case when no rows are returned.
IF SQLCODE = -100
DISPLAY 'No data found for the given account number.'
END-IF.
-911 Deadlock or timeout Resolve deadlocks, optimize transactions, adjust timeout settings.
Unique constraint
-803 Ensure no duplicate records are being inserted or updated.
violation
-805 Invalid host variable Declare host variables correctly and match data types.
-305 Null value in host variable Avoid inserting NULLs in non-nullable columns.
SQLCODE Error Description Resolution
-100 No data found Handle the case when no rows are returned in your queries.
FILE ABENDS –
Root Cause:
o This error occurs when the program fails to open a file. This is often caused by the file
being missing, locked, or permissions issues.
o Other possible causes include specifying the wrong file name or path or the file not
being accessible due to system-related problems.
File Status Code 35 Example:
arduino
Copy code
FILE STATUS = '35', ERROR: File cannot be opened.
Possible Causes:
o The file does not exist.
o Insufficient permissions to open the file.
o The file is locked by another process.
Resolution:
o Verify that the file exists at the specified location.
o Ensure that the correct file name, path, and access permissions are provided.
o Ensure that the file is not locked by another process.
o Check the system logs for any errors related to file access.
Example Code:
cobol
Copy code
OPEN INPUT MY-FILE
IF FILE-STATUS = '35' THEN
DISPLAY 'Error: File cannot be opened. Please check file availability and
permissions.'
STOP RUN.
END-IF.
java
Copy code
FILE STATUS = '39', ERROR: End of File (EOF) reached unexpectedly.
Possible Causes:
o The program reads more records than available.
o A loop reading the file does not check for the EOF condition.
o Incorrect handling of file pointers.
Resolution:
o Implement proper EOF checks before attempting to read a record.
o Make sure that you handle the EOF condition explicitly in the code.
Example Code:
cobol
Copy code
READ MY-FILE INTO MY-REC
AT END
DISPLAY 'End of file reached.'
CLOSE MY-FILE
STOP RUN.
NOT AT END
DISPLAY 'Processing Record: ' MY-REC
END-READ.
3. File Status Code 47: Invalid File Key or Invalid Key Operation
Root Cause:
o This error occurs when performing operations (e.g., READ, START, REWRITE) on a keyed
file (indexed file) with an invalid or incorrect key.
o It happens when the key used in the file operation doesn't match any key in the file, or
when an inappropriate operation is performed on the file.
File Status Code 47 Example:
java
Copy code
FILE STATUS = '47', ERROR: Invalid file key or invalid key operation.
Possible Causes:
o Incorrect key is used in the READ or START operation.
o The index file is corrupted.
Resolution:
o Ensure that the key is valid before performing file operations.
o Check that the file is correctly indexed.
o Handle invalid key conditions properly to avoid such errors.
Example Code:
cobol
Copy code
START MY-FILE KEY IS '12345'
IF FILE-STATUS = '47' THEN
DISPLAY 'Error: Invalid file key or operation.'
CLOSE MY-FILE
STOP RUN.
END-IF.
Root Cause:
o This error occurs when you attempt a READ or WRITE operation on a file that has not
been opened.
o It could happen if the file is not opened correctly due to earlier errors or if the file
pointer is not positioned properly before performing the operation.
File Status Code 22 Example:
arduino
Copy code
FILE STATUS = '22', ERROR: File not opened.
Possible Causes:
o The file open operation failed or was skipped.
o Incorrect file mode used during file open (e.g., trying to read when opened for writing
only).
Resolution:
o Ensure the file is properly opened before any operations.
o Check the FILE STATUS after the OPEN statement to verify successful opening.
Example Code:
cobol
Copy code
OPEN INPUT MY-FILE
IF FILE-STATUS = '22' THEN
DISPLAY 'Error: File was not opened. Check file availability.'
STOP RUN.
END-IF.
Root Cause:
o This error occurs when a program tries to write to a file that was opened in READ-ONLY
mode.
o The file might have been mistakenly opened in the wrong mode, causing the write
operation to fail.
File Status Code 30 Example:
arduino
Copy code
FILE STATUS = '30', ERROR: Write attempt on a read-only file.
Possible Causes:
o Incorrect file mode specified during the file open operation.
Resolution:
o Ensure that the file is opened in the correct mode based on the operation being
performed.
o Use the OPEN OUTPUT or OPEN I/O for write operations.
Example Code:
cobol
Copy code
OPEN I-O MY-FILE
IF FILE-STATUS = '30' THEN
DISPLAY 'Error: Attempt to write to a read-only file.'
STOP RUN.
END-IF.
Root Cause:
o This error occurs when the file specified cannot be found at the location provided.
o It typically occurs when a file is not present in the directory or the filename is
misspelled.
File Status Code 21 Example:
arduino
Copy code
FILE STATUS = '21', ERROR: File not found.
Possible Causes:
o The file does not exist at the specified path.
o Incorrect file name or path.
Resolution:
o Check if the file exists in the specified directory.
o Ensure the file name and path are correct.
Example Code:
cobol
Copy code
OPEN INPUT MY-FILE
IF FILE-STATUS = '21' THEN
DISPLAY 'Error: File not found. Please check the file path.'
STOP RUN.
END-IF.
Root Cause:
o This error occurs when there is a mismatch between the access mode specified in the
FILE-CONTROL section and the actual access mode used in the OPEN statement.
File Status Code 34 Example:
java
Copy code
FILE STATUS = '34', ERROR: Invalid access mode for the file.
Possible Causes:
o The file is opened in a mode that doesn't match its declared mode in the FILE-CONTROL
section.
Resolution:
o Ensure the access mode in the FILE-CONTROL section matches the mode used in the
OPEN statement.
Example Code:
cobol
Copy code
OPEN I-O MY-FILE
IF FILE-STATUS = '34' THEN
DISPLAY 'Error: Invalid access mode for the file.'
STOP RUN.
END-IF.
File Status
Error Description Resolution
Code
47 Invalid file key or key Ensure the key is valid and the file is correctly indexed.
File Status
Error Description Resolution
Code
operation
Verify the file exists at the correct location and the filename
21 File not found
is correct.
VSAM PARAMETERS –
In the context of VSAM, a Control Interval (CI) is the smallest unit of data that can be read or
written. The CI Size specifies the amount of space that is allocated for each CI, which is
important for how data is stored and accessed.
Control Interval is the physical storage unit that holds multiple records.
The CI Size helps define how many records can fit into one CI.
CI Size Importance:
A larger CI size generally improves performance by reducing the number of I/O operations
because more records can be accessed in a single read/write operation.
A smaller CI size may improve space efficiency in situations where the dataset consists of small
records, but it might increase the number of I/O operations.
The CI size can be defined during the dataset definition in IDCAMS using the CI_SIZE
parameter.
Example:
jcl
Copy code
//MYJOB JOB (ACCT),'MY JOB',CLASS=A,MSGCLASS=A
//STEP1 EXEC PGM=IDCAMS
//SYSPRINT DD SYSOUT=*
//MYDATA DD DSN=MY.VSAM.KSDS,DISP=SHR
//SYSIN DD *
DEFINE CLUSTER (NAME(MY.VSAM.KSDS) -
ORGANIZATION INDEXED -
RECFM=FB -
LRECL=80 -
RECORDKEY(KEY,10) -
DATA(REC-AREA) -
CI_SIZE=4096)
/*
While RTPD (Record Type Physical Data) is not a standard, recognized parameter in IBM’s
official VSAM documentation, it seems to refer to the physical storage of records in VSAM
files. This might include concepts such as the format in which records are stored, block
structure, and the physical representation of records on disk.
This could also be related to the record format (RECFM) and how data is arranged in the
dataset's control intervals and control areas.
RECFM: Defines how records are stored (e.g., FB for Fixed Block, VB for Variable Block).
LRECL: The logical record length, which defines the length of a record.
CI/CA Sizes: These define how records are grouped and stored at the physical level in Control
Intervals (CIs) and Control Areas (CAs).
Here are some additional key parameters that control the physical storage and access methods for
VSAM datasets:
RECFM (Record Format):
Defines the length of each record in the dataset. For a fixed block, this is the fixed size for each
record.
For a variable record format, LRECL refers to the maximum possible size of the records.
Space (SPACE):
Specifies the space allocation for the VSAM file, including primary and secondary space. This is
crucial for determining how much storage is initially allocated and how the dataset grows over
time.
KEYLEN specifies the length of the key in a KSDS (Key Sequenced Data Set).
KEYPOS specifies the position of the key in the record. These parameters are critical for
maintaining the integrity of indexed datasets.
A Control Area (CA) is a logical group of one or more Control Intervals (CIs). The CA size defines
how large the Control Area is, impacting how records are grouped together in the dataset.
Larger CA sizes help in reducing I/O operations since more records are grouped together.
Conclusion
To summarize:
CI Size and CA Size are critical parameters for controlling the physical layout of records within
VSAM datasets, affecting performance and efficiency.
While RTPD (Record Type Physical Data) isn't a specific IBM VSAM parameter, it likely refers to
how records are stored physically in the dataset, which is influenced by RECFM, LRECL, CI/CA
sizes, and other parameters like KEYLEN and KEYPOS.
Proper configuration of these parameters is essential for optimizing I/O performance, managing
space, and ensuring efficient data access in VSAM datasets.
OCCURS –
Program Overview:
The program uses an OCCURS clause to store multiple customer account records.
Each record consists of an account number, account holder's name, and account balance.
The program will process and display the account information for each customer.
COBOL Code:
cobol
Copy code
IDENTIFICATION DIVISION.
PROGRAM-ID. BANKACCOUNTS.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT ACCOUNT-FILE ASSIGN TO 'SYSIN'.
DATA DIVISION.
FILE SECTION.
FD ACCOUNT-FILE.
01 ACCOUNT-RECORD.
05 CUSTOMER-ID PIC 9(6).
05 CUSTOMER-NAME PIC A(20).
05 ACCOUNT-BALANCE PIC 9(8)V99.
WORKING-STORAGE SECTION.
01 WS-CUSTOMER-COUNT PIC 9(3) VALUE 0.
01 WS-DISPLAY-MESSAGE PIC X(50) VALUE "Processing Banking Records...".
01 WS-END-FLAG PIC X VALUE 'N'.
01 CUSTOMER-TABLE.
05 CUSTOMER OCCURS 5 TIMES INDEXED BY CUST-INDEX.
10 CUSTOMER-ID PIC 9(6).
10 CUSTOMER-NAME PIC A(20).
10 ACCOUNT-BALANCE PIC 9(8)V99.
PROCEDURE DIVISION.
READ-ACCOUNTS.
READ ACCOUNT-FILE INTO ACCOUNT-RECORD
AT END
MOVE 'Y' TO WS-END-FLAG
NOT AT END
ADD 1 TO WS-CUSTOMER-COUNT
MOVE CUSTOMER-ID TO CUSTOMER(WS-CUSTOMER-COUNT)
MOVE CUSTOMER-NAME TO CUSTOMER(WS-CUSTOMER-COUNT)
MOVE ACCOUNT-BALANCE TO CUSTOMER(WS-CUSTOMER-COUNT).
DISPLAY-ACCOUNTS.
DISPLAY "Customer Account Details:"
PERFORM VARYING CUST-INDEX FROM 1 TO WS-CUSTOMER-COUNT
DISPLAY "Account Number: " CUSTOMER(CUST-INDEX).CUSTOMER-ID
DISPLAY "Customer Name: " CUSTOMER(CUST-INDEX).CUSTOMER-NAME
DISPLAY "Account Balance: " CUSTOMER(CUST-INDEX).ACCOUNT-
BALANCE
DISPLAY "-------------------------------"
END-PERFORM.
1. IDENTIFICATION DIVISION:
o Specifies the name of the program (BANKACCOUNTS).
2. ENVIRONMENT DIVISION:
o Specifies the input file (ACCOUNT-FILE) where the account records are stored.
o In real banking systems, this would point to a file or a database table.
3. DATA DIVISION:
o FILE SECTION: Defines the structure of the input file (ACCOUNT-FILE), which contains
customer account information.
The account file is structured with fields such as CUSTOMER-ID, CUSTOMER-
NAME, and ACCOUNT-BALANCE.
o WORKING-STORAGE SECTION:
WS-CUSTOMER-COUNT: Keeps track of the number of customer records
processed.
CUSTOMER-TABLE: Defines an array of customer records (using the OCCURS
clause). It allows the program to handle multiple customer records in memory.
WS-END-FLAG: A flag to indicate when all records have been processed.
4. PROCEDURE DIVISION:
o OPEN INPUT: Opens the file (ACCOUNT-FILE) for reading.
o READ-ACCOUNTS: Reads each record from the input file and populates the CUSTOMER-
TABLE using the OCCURS clause. The READ statement continues until all records are
processed (when WS-END-FLAG is set to 'Y').
Each record from the input file is moved into the CUSTOMER-TABLE array for
further processing.
o DISPLAY-ACCOUNTS: Loops through the CUSTOMER-TABLE array using a PERFORM
VARYING loop and displays each customer's account number, name, and balance.
5. CLOSE ACCOUNT-FILE: Closes the input file after processing all records.
Output Example:
yaml
Copy code
123456 John Doe 5000.50
234567 Alice Smith 1500.75
345678 Bob Johnson 3200.40
The output would look like:
yaml
Copy code
Processing Banking Records...
Customer Account Details:
Account Number: 123456
Customer Name: John Doe
Account Balance: 5000.50
-------------------------------
Account Number: 234567
Customer Name: Alice Smith
Account Balance: 1500.75
-------------------------------
Account Number: 345678
Customer Name: Bob Johnson
Account Balance: 3200.40
-------------------------------
OCCURS Clause: The OCCURS clause is used to create a table of multiple records. Each record
holds information such as account number, name, and balance. The table can be accessed via
the INDEXED BY index (e.g., CUST-INDEX).
READ and MOVE Statements: These are used to read data from an input file and populate the
table. Each account’s details are stored in the array (using MOVE) for later processing.
PERFORM VARYING Loop: This loop is used to process and display each record from the table in
sequential order.
This kind of processing is common in banking systems, especially for managing and displaying
customer data such as:
This example showcases how COBOL’s OCCURS clause can be used to handle and display
multiple records, which is a common use case in the banking domain for processing large
volumes of repetitive data.
COBOL ABENDS -
Resolution:
Example:
cobol
Copy code
IDENTIFICATION DIVISION.
PROGRAM-ID. FILEACCESS.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT INPUT-FILE ASSIGN TO 'MY.DATA.FILE'
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD INPUT-FILE.
01 INPUT-RECORD.
05 CUSTOMER-ID PIC 9(6).
05 CUSTOMER-NAME PIC X(20).
05 ACCOUNT-BALANCE PIC 9(8)V99.
WORKING-STORAGE SECTION.
01 WS-END-FLAG PIC X VALUE 'N'.
PROCEDURE DIVISION.
OPEN INPUT INPUT-FILE
IF FILE-STATUS NOT EQUAL TO '00'
DISPLAY 'ERROR: File open failed'
STOP RUN
END-IF.
CLOSE INPUT-FILE.
STOP RUN.
Explanation: In this example, if the file MY.DATA.FILE is not available or accessible, the
program will end with an OPEN ERROR ABEND. The error handling ensures that the program
gracefully handles file access issues by checking the FILE-STATUS.
Resolution:
Ensure that array indices are within the defined range of the OCCURS clause.
Validate indices before using them.
Example:
cobol
Copy code
IDENTIFICATION DIVISION.
PROGRAM-ID. ARRAYACCESS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CUSTOMER-TABLE.
05 CUSTOMER OCCURS 10 TIMES INDEXED BY CUST-INDEX.
10 CUSTOMER-ID PIC 9(6).
10 CUSTOMER-NAME PIC X(20).
10 ACCOUNT-BALANCE PIC 9(8)V99.
PROCEDURE DIVISION.
MOVE 1 TO WS-INDEX.
PERFORM FILL-TABLE
PERFORM DISPLAY-TABLE.
STOP RUN.
FILL-TABLE.
PERFORM VARYING CUST-INDEX FROM 1 BY 1 UNTIL CUST-INDEX > 10
MOVE 'Customer ' TO CUSTOMER(CUST-INDEX).CUSTOMER-NAME
MOVE 1000.00 TO CUSTOMER(CUST-INDEX).ACCOUNT-BALANCE
END-PERFORM.
DISPLAY-TABLE.
PERFORM VARYING CUST-INDEX FROM 1 BY 1 UNTIL CUST-INDEX > 10
DISPLAY CUSTOMER(CUST-INDEX).CUSTOMER-ID
DISPLAY CUSTOMER(CUST-INDEX).CUSTOMER-NAME
DISPLAY CUSTOMER(CUST-INDEX).ACCOUNT-BALANCE
END-PERFORM.
Explanation: In this example, an invalid subscript, such as a value greater than 10, would cause
an ABEND (SUBSCRIPT OUT OF RANGE). To avoid this, we ensure that the OCCURS clause
defines a limit, and the index is always checked before use.
3. DIVISION BY ZERO
ABEND: SOC7 (or S0C7) is a common abend code when performing an invalid arithmetic
operation, such as dividing by zero.
Root Cause: This occurs when a DIVIDE operation is performed where the divisor is zero or
contains an invalid value.
Resolution:
Always check that the divisor is not zero before performing the division operation.
Example:
cobol
Copy code
IDENTIFICATION DIVISION.
PROGRAM-ID. DIVBYZERO.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUMERATOR PIC 9(5) VALUE 100.
01 DENOMINATOR PIC 9(5) VALUE 0.
01 RESULT PIC 9(5).
PROCEDURE DIVISION.
IF DENOMINATOR = 0
DISPLAY 'ERROR: Division by zero is not allowed.'
ELSE
DIVIDE NUMERATOR BY DENOMINATOR GIVING RESULT
DISPLAY 'The result is: ' RESULT
END-IF.
STOP RUN.
Explanation: In this example, we check if the DENOMINATOR is zero before performing the
division operation. If it's zero, an error message is displayed, and the division is avoided to
prevent the S0C7 abend.
4. INVALID DATA (Data Corruption or Incorrect Format)
Resolution:
Ensure that all data is correctly initialized and validated before being used.
Validate input formats and check for null values or invalid data types.
Example:
cobol
Copy code
IDENTIFICATION DIVISION.
PROGRAM-ID. INVALIDDATA.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CUSTOMER-ID PIC 9(6).
01 CUSTOMER-NAME PIC A(20).
PROCEDURE DIVISION.
MOVE '123456' TO CUSTOMER-ID
MOVE 'John Doe' TO CUSTOMER-NAME
IF CUSTOMER-ID IS NUMERIC
DISPLAY 'Valid Customer ID: ' CUSTOMER-ID
ELSE
DISPLAY 'Invalid Customer ID'
END-IF.
STOP RUN.
Explanation: In this program, we ensure that the CUSTOMER-ID is a valid numeric value
before processing. If it’s not numeric, we catch the error and prevent access to invalid data, thus
avoiding potential invalid data errors or memory corruption.
6. INSPECT/STRING/UNSTRING ABENDS
Resolution:
Ensure that buffers are large enough to hold the manipulated data and that data formats are
consistent.
Example:
cobol
Copy code
IDENTIFICATION DIVISION.
PROGRAM-ID. STRINGABEND.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 FULL-NAME PIC A(50).
01 FIRST-NAME PIC A(20).
01 LAST-NAME PIC A(20).
PROCEDURE DIVISION.
MOVE 'John Doe' TO FULL-NAME
STOP RUN.
Explanation: The program uses the STRING statement to split the FULL-NAME into FIRST-
NAME and LAST-NAME. The ON EXCEPTION clause ensures that the program doesn’t
ABEND if the string manipulation fails.
cobol
Copy code
IDENTIFICATION DIVISION.
PROGRAM-ID. MAIN.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUM1 PIC 9(3) VALUE 10. * Define NUM1 with value 10
01 NUM2 PIC 9(3) VALUE 20. * Define NUM2 with value 20
01 RESULT PIC 9(3). * Define RESULT to store the result of
addition
PROCEDURE DIVISION.
CALL 'ADDITION' USING NUM1 NUM2 RESULT * Call the 'ADDITION'
program statically
DISPLAY 'The result is: ' RESULT * Display the result
STOP RUN. * End the main program
cobol
Copy code
IDENTIFICATION DIVISION.
PROGRAM-ID. ADDITION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUM1 PIC 9(3). * Declare NUM1
01 NUM2 PIC 9(3). * Declare NUM2
01 RESULT PIC 9(3). * Declare RESULT to store the sum
PROCEDURE DIVISION.
ADD NUM1 TO NUM2 GIVING RESULT. * Add NUM1 and NUM2 and store in
RESULT
EXIT. * Exit the subprogram after
performing addition
cobol
Copy code
IDENTIFICATION DIVISION.
PROGRAM-ID. MAIN.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUM1 PIC 9(3) VALUE 10. * Initialize NUM1 with 10
01 NUM2 PIC 9(3) VALUE 20. * Initialize NUM2 with 20
01 RESULT PIC 9(3). * Define RESULT to hold addition result
01 SUBPROGRAM-NAME PIC X(8) VALUE 'ADDITION'. * Subprogram name
stored in this variable
PROCEDURE DIVISION.
CALL SUBPROGRAM-NAME USING NUM1 NUM2 RESULT * Dynamically call
the subprogram
DISPLAY 'The result is: ' RESULT * Display the result after
dynamic call
STOP RUN. * End the main program
cobol
Copy code
IDENTIFICATION DIVISION.
PROGRAM-ID. ADDITION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUM1 PIC 9(3). * Declare NUM1
01 NUM2 PIC 9(3). * Declare NUM2
01 RESULT PIC 9(3). * Declare RESULT
PROCEDURE DIVISION.
ADD NUM1 TO NUM2 GIVING RESULT. * Perform addition of NUM1 and
NUM2
EXIT. * Exit the subprogram after
calculation
STRING Example:
cobol
Copy code
IDENTIFICATION DIVISION.
PROGRAM-ID. STRINGEXAMPLE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 FULL-NAME PIC A(50). * Declare a variable to store the full
name
01 FIRST-NAME PIC A(20). * Declare a variable for the first name
01 LAST-NAME PIC A(20). * Declare a variable for the last name
PROCEDURE DIVISION.
MOVE 'John Doe' TO FULL-NAME * Assign 'John Doe' to FULL-NAME
Explanation:
The STRING statement splits FULL-NAME into FIRST-NAME and LAST-NAME based on the
space delimiter.
INSPECT Example:
cobol
Copy code
IDENTIFICATION DIVISION.
PROGRAM-ID. INSPECTEXAMPLE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 STRING-TO-INSPECT PIC X(50) VALUE 'HELLO WORLD'. * Declare a string
for inspection
01 COUNT-VAR PIC 9(2). * Declare variable to store count result
PROCEDURE DIVISION.
INSPECT STRING-TO-INSPECT
TALLYING COUNT-VAR FOR ALL 'O' * Count how many 'O'
characters are in STRING-TO-INSPECT
DISPLAY 'Number of O''s: ' COUNT-VAR * Display the count of 'O'
characters in the string
Explanation:
The INSPECT statement counts how many times the letter 'O' appears in the string STRING-
TO-INSPECT.
REDEFINES Example:
The REDEFINES clause allows one variable to represent multiple structures sharing the same
memory location.
cobol
Copy code
IDENTIFICATION DIVISION.
PROGRAM-ID. REDEFINES_EXAMPLE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CUSTOMER-RECORD.
05 CUSTOMER-ID PIC 9(5). * Define a 5-digit customer ID
05 CUSTOMER-NAME PIC A(10). * Define a 10-character customer
name
01 CUSTOMER-NAME-REDEF REDEFINES CUSTOMER-NAME.
05 FIRST-NAME PIC A(5). * Redefine the first 5 characters
as FIRST-NAME
05 LAST-NAME PIC A(5). * Redefine the next 5 characters as
LAST-NAME
PROCEDURE DIVISION.
MOVE 'John Doe' TO CUSTOMER-NAME * Assign 'John Doe' to CUSTOMER-
NAME
DISPLAY 'First Name: ' FIRST-NAME * Display first name extracted
from CUSTOMER-NAME
DISPLAY 'Last Name: ' LAST-NAME * Display last name extracted
from CUSTOMER-NAME
STOP RUN. * End the program
Explanation:
RENAME Example:
The RENAME clause allows you to give a different name to an existing variable.
cobol
Copy code
IDENTIFICATION DIVISION.
PROGRAM-ID. RENAME_EXAMPLE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CUSTOMER-ID PIC 9(5). * Define customer ID variable
01 CUSTOMER-NAME PIC A(20) RENAMES CUSTOMER-ID. * Rename CUSTOMER-
NAME to CUSTOMER-ID
PROCEDURE DIVISION.
MOVE '12345' TO CUSTOMER-ID * Assign '12345' to CUSTOMER-ID
DISPLAY 'Customer ID: ' CUSTOMER-ID * Display customer ID
STOP RUN. * End the program
Explanation:
cobol
Copy code
IDENTIFICATION DIVISION.
PROGRAM-ID. REFMOD.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 STRING-VALUE PIC A(20) VALUE 'HELLO WORLD'. * Declare and
initialize STRING-VALUE
PROCEDURE DIVISION.
DISPLAY 'First 5 characters: ' STRING-VALUE(1:5) * Access first
5 characters of STRING-VALUE
DISPLAY 'Last 5 characters: ' STRING-VALUE(11:5) * Access last 5
characters of STRING-VALUE
STOP RUN. * End the
program
Explanation:
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT INPUT-FILE ASSIGN TO 'MY.DATA.FILE'
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD INPUT-FILE.
01 INPUT-RECORD.
05 CUSTOMER-NAME PIC A(20). * Define customer name field
05 CUSTOMER-ID PIC 9(6). * Define customer ID field
WORKING-STORAGE SECTION.
01 EOF-FLAG PIC X VALUE 'N'. * Flag to indicate end of
file
PROCEDURE DIVISION.
OPEN INPUT INPUT-FILE * Open the input file for reading
cobol
Copy code
IDENTIFICATION DIVISION.
PROGRAM-ID. IFELSEEXAMPLE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 AGE PIC 9(3). * Variable to hold the age of a person
01 MESSAGE PIC A(50). * Variable to hold the message based on age
PROCEDURE DIVISION.
MOVE 25 TO AGE * Assign 25 to AGE variable
IF AGE >= 18 THEN
MOVE 'Adult' TO MESSAGE * If AGE is greater than or equal to
18, assign 'Adult' to MESSAGE
ELSE
MOVE 'Minor' TO MESSAGE * Otherwise, assign 'Minor' to
MESSAGE
END-IF.
DISPLAY MESSAGE * Display the message based on the IF-
ELSE condition
STOP RUN. * End the program
Explanation:
The program checks if the person's age is 18 or older. If true, it prints "Adult"; otherwise,
it prints "Minor".
The PERFORM statement is used to execute a paragraph or a section of code. Here’s an example
using it:
cobol
Copy code
IDENTIFICATION DIVISION.
PROGRAM-ID. PERFORMEXAMPLE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUM1 PIC 9(3) VALUE 10. * Declare NUM1 with value 10
01 NUM2 PIC 9(3) VALUE 20. * Declare NUM2 with value 20
01 RESULT PIC 9(3). * Declare RESULT to store the sum
PROCEDURE DIVISION.
PERFORM ADD-NUMBERS * Call the ADD-NUMBERS paragraph using
PERFORM
DISPLAY 'The sum is: ' RESULT * Display the result of the
addition
STOP RUN. * End the program
Explanation:
The PERFORM ADD-NUMBERS executes the ADD-NUMBERS paragraph, where NUM1 and
NUM2 are added, and the result is stored in the RESULT variable.
In a COBOL program interacting with DB2, the SQL statements will be used to read and
manipulate data in the database. Below is an example where we retrieve and display data from a
DB2 table.
cobol
Copy code
IDENTIFICATION DIVISION.
PROGRAM-ID. DB2EXAMPLE.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT DB2FILE ASSIGN TO DATABASE.
DATA DIVISION.
FILE SECTION.
FD DB2FILE.
01 DB2RECORD.
05 CUSTOMER-ID PIC 9(6).
05 CUSTOMER-NAME PIC A(50).
WORKING-STORAGE SECTION.
01 SQLCODE PIC S9(9) COMP-5.
PROCEDURE DIVISION.
OPEN INPUT DB2FILE * Open the DB2 file for input
FETCH DB2RECORD INTO DB2FILE * Retrieve a record from the DB2
table
DISPLAY 'Customer ID: ' CUSTOMER-ID
DISPLAY 'Customer Name: ' CUSTOMER-NAME
CLOSE DB2FILE * Close the DB2 file after retrieval
STOP RUN.
Explanation:
The program interacts with a DB2 file (or database table), fetches a record, and displays
the CUSTOMER-ID and CUSTOMER-NAME.