Db2 in practice

Contents
The cursor loop
Checking SQLCA
Commit and rollback

The cursor loop

The workhorse of mainframe reporting: declare a cursor, open it, fetch rows in a loop until SQLCODE 100, then close. Every statement checks SQLCODE before the next one runs:

EXEC SQL DECLARE C1 CURSOR FOR SELECT EMP-ID, SALARY FROM EMPLOYEE WHERE DEPT = :WS-DEPT END-EXEC. EXEC SQL OPEN C1 END-EXEC. PERFORM UNTIL SQLCODE = 100 EXEC SQL FETCH C1 INTO :WS-EMP-ID, :WS-SALARY END-EXEC IF SQLCODE = 0 PERFORM PROCESS-ROW END-IF END-PERFORM. EXEC SQL CLOSE C1 END-EXEC.

Checking SQLCA

SQLCA is the communication area copied into WORKING-STORAGE with INCLUDE SQLCA. SQLCODE 0 is success, 100 is no more rows, and any negative code means the statement failed and the transaction should roll back.

Commit and rollback

End a unit of work with EXEC SQL COMMIT END-EXEC, and undo it with EXEC SQL ROLLBACK END-EXEC. Commit at a business boundary, not after every row, so the database stays consistent if the job fails halfway.

Next: Capstone: CLI calculator

Article author: Arthur Isaev

Related articles
COBOL
COBOL course: zero to hero in 50 lessons
What is COBOL and where it runs
Install COBOL and check the version
cobc: the COBOL compiler
Your first COBOL program
The four divisions
Variables and PICTURE clauses
Numbers: USAGE, COMP and COMP-3
Alphanumeric fields (strings)
Constants: level 78 and VALUE
Conditions and relation tests
IF, ELSE and EVALUATE
PERFORM loops
Tables: OCCURS arrays
INDEXED BY and OCCURS DEPENDING ON
Paragraphs and sections
STRING and UNSTRING
Files: SELECT and FD
Sequential files: READ and WRITE
Error handling: USE and DECLARATIVES
Copybooks and COPY
OO COBOL: classes and objects
Inheritance in OO COBOL
Intrinsic functions
COBOL idioms and clean code
Build tools: make and cobc flags
Testing COBOL programs
Debugging: cobcdebug and -g
Encodings: EBCDIC, ASCII and UTF-8
Date and time
JSON and XML
Calling C functions
Subprograms: CALL and USING
SQL with EXEC SQL
DISPLAY and formatting
ACCEPT and the SCREEN section
The Report Writer
Sorting with the SORT verb
Command-line arguments
Environment and RETURN-CODE
Logging to files
CSV parsing
CICS overview
Batch processing and JCL
File status and transactions
Db2 in practice
Capstone: CLI calculator
Capstone: fixed-width file processor
Performance: tuning COBOL
Modern COBOL tour (2014+)
Hero roadmap: the whole course on one page
Install COBOL on Windows 11
Install COBOL on Ubuntu
Install COBOL on Rocky Linux
Install COBOL on macOS
Install COBOL on FreeBSD
COBOL in Docker
COBOL on mainframes
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: