Important Mainframe Concepts
Important Mainframe Concepts
SQL Code -811 occurs in DB2 when a query retrieves more than one row, but the context of
the query or the way it's written expects only one row.
FETCH FIRST ROW ONLY limits the number of rows returned by the query to just one. You can
use it when you are not concerned about which specific row to retrieve or after sorting to get a
specific row.
If multiple rows are valid, modify the program to handle them using a cursor.
EXEC SQL
DECLARE EMP_CURSOR CURSOR FOR
SELECT EMP_ID, NAME
FROM EMPLOYEE
WHERE DEPT = 'SALES'
END-EXEC.
EXEC SQL
OPEN EMP_CURSOR
END-EXEC.
EXEC SQL
CLOSE EMP_CURSOR
END-EXEC.
PASSING DATA FROM JCL TO COBOL USING PARM -
1. Set Up the JCL
In the JCL, use the PARM keyword in the EXEC statement to pass the parameter value to the
COBOL program.
In COBOL, the PARM value is accessed through the LINKAGE SECTION and the PROCEDURE
DIVISION USING clause.
IDENTIFICATION DIVISION.
PROGRAM-ID. COBOLPGM.
ENVIRONMENT DIVISION.
DATA DIVISION.
LINKAGE SECTION.
01 PARM-DATA.
05 PARM-LENGTH PIC S9(4) COMP. *> Length of the parameter
05 PARM-VALUE PIC X(100). *> Actual parameter value
1. LINKAGE SECTION:
o PARM-LENGTH: Holds the length of the parameter passed.
o PARM-VALUE: Holds the actual parameter value. Use a sufficient size to accommodate
the expected parameter length.
WORKING-STORAGE SECTION.
01 STUDENT-NAMES.
05 STUDENT-NAME PIC X(20) OCCURS 5 TIMES.
PROCEDURE DIVISION.
MOVE 'ALICE' TO STUDENT-NAME(1).
MOVE 'BOB' TO STUDENT-NAME(2).
DISPLAY STUDENT-NAME(1).
DISPLAY STUDENT-NAME(2).
2. Dynamic OCCURS
Definition: The number of occurrences is variable and determined at runtime using the
DEPENDING ON clause.
Declaration: The array size is specified as a range (1 TO m) and depends on a control variable.
Access: The control variable determines the valid range of indices at runtime.
Use Case: Useful when the number of elements changes dynamically based on runtime
conditions.
WORKING-STORAGE SECTION.
01 STUDENT-NAMES.
05 NUM-STUDENTS PIC 9(2). *> Control variable
05 STUDENT-NAME PIC X(20) OCCURS 1 TO 10 TIMES
DEPENDING ON NUM-STUDENTS.
Accessing Dynamic OCCURS
PROCEDURE DIVISION.
MOVE 3 TO NUM-STUDENTS. *> Array has 3 elements
MOVE 'ALICE' TO STUDENT-NAME(1).
MOVE 'BOB' TO STUDENT-NAME(2).
MOVE 'CHARLIE' TO STUDENT-NAME(3).
1D (One-Dimensional) OCCURS
Declaration
WORKING-STORAGE SECTION.
01 STUDENT-NAMES.
05 STUDENT-NAME PIC X(20) OCCURS 5 TIMES.
Accessing Elements
cobol
Copy code
PROCEDURE DIVISION.
MOVE 'ALICE' TO STUDENT-NAME(1).
MOVE 'BOB' TO STUDENT-NAME(2).
DISPLAY STUDENT-NAME(1).
DISPLAY STUDENT-NAME(2).
2D (Two-Dimensional) OCCURS
Declaration
WORKING-STORAGE SECTION.
01 SALES-TABLE.
05 REGION OCCURS 3 TIMES.
10 MONTH OCCURS 12 TIMES PIC 9(5).
Accessing Elements
You access elements of a 2D array using two subscripts: one for the outer dimension (REGION)
and one for the inner dimension (MONTH).
PROCEDURE DIVISION.
MOVE 100 TO SALES-TABLE(1, 1). *> Region 1, Month 1
MOVE 200 TO SALES-TABLE(2, 6). *> Region 2, Month 6
MOVE 300 TO SALES-TABLE(3, 12). *> Region 3, Month 12
Output:
Key Differences: 1D vs 2D
Aspect 1D OCCURS 2D OCCURS
Use Case List of names, numbers, etc. Tabular data like sales, temperatures, etc.
1D Array
2D Array
For 2D arrays, use nested loops to iterate over rows and columns.
Best Practices
Syntax:
Example:
Opening the cursor executes the SELECT query and positions it before the first row of the result
set.
Syntax:
OPEN cursor_name;
Example:
OPEN employee_cursor;
The FETCH statement retrieves one row at a time from the cursor. You can move the cursor using
specific fetch options.
Syntax:
Example:
Use the data retrieved from the cursor in your application logic (e.g., display or calculations).
Example:
When the cursor is no longer needed, close it to release the associated resources.
Syntax:
CLOSE cursor_name;
Example:
CLOSE employee_cursor;
EXEC SQL
OPEN employee_cursor
END-EXEC.
EXEC SQL
CLOSE employee_cursor
END-EXEC.