Skip to main content
This guide runs a batch program that updates a Db2 table, keeps the state it left, and reads it back. Needs harwell:runs:write, which includes the harwell:runs:read tools used here.

1. Seed the tables

DB/seed.sql holds the DDL and rows a fresh volume starts with.
DB/seed.sql:

2. Run SQL from a program

Progs/RAISE.cbl:
Procs/RAISEJOB.jcl:
run.json:

3. Keep the volume

Each call gets a fresh volume, discarded when the call ends, unless you keep it. Set return_work:
The step displays BAL=000501000. The result names the kept volume:

4. Query the tables

sql runs one SELECT, WITH or VALUES statement in Db2’s dialect, read-only. A statement that fails returns Db2’s SQLCODE and SQLSTATE: -204 for an unknown table, -206 for an unknown column, -104 for a syntax error.

5. List the data sets

Each data set comes with its organization, RECFM, LRECL, bytes and record count, and the Db2 tables beside them. In a pattern, * matches within one qualifier, ** across qualifiers and % one character.

6. Read records

Each record comes as text and hex. Give a copybook to decode fields, including COMP-3, COMP and zoned decimal:
copybook is the copybook’s source text. Pass copybooks ({MEMBER: source}) for each COPY it contains. A KSDS or RRDS a program wrote is refused: REPRO it to a sequential data set in a job, then read that.

7. Carry on from the volume

Pass the work_id to the next call. It starts from the state the last run left:
The balance is now 502000. To carry state without passing ids, use a system.

SQLCODEs in a result

Every SQL statement that ends with a negative SQLCODE is a warning in the result’s diagnostics, at the program’s line, whether or not the program checks SQLCODE:
One line per statement and code, with the count when it failed more than once. The text is IBM’s for the codes Db2 defines. The step’s return code stays the program’s own. See Errors.

SQL in CALLed programs

A subprogram with EXEC SQL, CALLed from a batch or online program, runs its SQL on the caller’s unit of work, as on z/OS.