After REPLACE versus RESUME, the next DB2 LOAD skill is describing the input record. Field specifications, WHEN, NULLIF, DEFAULTIF, generated and identity columns, XML, LOBs, and discard data sets are how you turn a sequential file into table rows without silently loading garbage.
A LOAD job can feed several tables from one SYSREC. Each INTO TABLE clause can have a WHEN field-selection criterion. IBM samples load EMP versus DEPT by testing a record type in a positional field. Records that do not satisfy WHEN for that table are not loaded there. If you requested discards, they can be written to the discard data set.
123456789LOAD DATA INDDN SYSREC INTO TABLE HR.EMPLOYEE WHEN (1:1) = 'E' (EMPNO POSITION(2:7) CHAR, LASTNAME POSITION(8:22) CHAR) INTO TABLE HR.DEPARTMENT WHEN (1:1) = 'D' (DEPTNO POSITION(2:4) CHAR, DEPTNAME POSITION(5:40) CHAR)
Positional WHEN uses start:end column numbers; column 1 is the first byte of the assembled record. Character, hex (X'...'), and graphic constants are the usual comparisons. WHEN SQL/DS='table-name' applies to SQL/DS formatted input. If you omit WHEN, LOAD attempts every record for that INTO TABLE.
Do not put LOB or XML fields in a WHEN criterion when you use FORMAT SPANNED YES. IBM forbids referencing those columns in the field-selection criterion because they sit in the spanned tail of the record.
A field specification names a column (or an IGNOREFIELDS dummy), gives POSITION(start:end) or POSITION(*), and a data type. If you omit POSITION, the field starts one column after the previous field. * means that next-byte start; *+n skips n bytes.
| Specification | Notes |
|---|---|
| CHAR(n) / VARCHAR | Character; VARCHAR has a 2-byte length prefix in binary layouts |
| INTEGER / SMALLINT / BIGINT | Binary integers, or INTEGER EXTERNAL(n) for character numbers |
| DECIMAL EXTERNAL / PACKED / ZONED | Display, packed, or zoned decimal |
| DATE/TIME/TIMESTAMP EXTERNAL | External datetime strings; length follows installation defaults if omitted |
| BLOBF / CLOBF / DBCLOBF | File-reference: field holds a file name for LOB data |
| XML / BINARYXML | XML document in the record or as binary XML |
FORMAT UNLOAD, FORMAT INTERNAL, and FORMAT SQL/DS do not allow field specifications—the format already defines every byte. FORMAT DELIMITED uses field names and types without classic POSITION ranges; fields are separated by COLDEL.
If you specify any field for a table, IBM requires a specification for every column that has no default. A NOT NULL column with no default and no field spec terminates the job. Columns you omit receive their default (including identity GENERATED BY DEFAULT if you leave them out).
123456INTO TABLE HR.EMPLOYEE (EMPNO POSITION(1:6) CHAR, FIRSTNME POSITION(7:18) CHAR, LASTNAME POSITION(19:33) CHAR, SALARY POSITION(34:42) DECIMAL EXTERNAL(9), HIREDATE POSITION(43:52) DATE EXTERNAL)
Both attach to a field and test a condition on the input (positional or field-name).
12345678910LOAD DATA INDDN SYSRECST CONTINUEIF(80:80)='X' RESUME YES INTO TABLE SYSIBM.SYSSTRINGS (INCCSID POSITION(1) INTEGER EXTERNAL(5), OUTCCSID POSITION(7) INTEGER EXTERNAL(5), TRANSTYPE POSITION(13) CHAR(2), ERRORBYTE POSITION(16) CHAR(1) NULLIF(ERRORBYTE=' '), SUBBYTE POSITION(18) CHAR(1) NULLIF(SUBBYTE=' '), TRANSPROC POSITION(20) CHAR(8), IBMREQD POSITION(29) CHAR(1), TRANSTAB POSITION(31) CHAR(256) DEFAULTIF(TRANSTYPE='GG'))
That IBM sample also shows CONTINUEIF: if column 80 is X, concatenate the next physical record before parsing fields. NULLIF and DEFAULTIF are not valid with FORMAT UNLOAD or FORMAT INTERNAL.
GENERATED ALWAYS columns are normally computed by Db2. Reloading an UNLOAD of a temporal table or an identity-keyed table needs an explicit override:
Identity and row-change timestamp columns may appear in the field list only when they are GENERATED BY DEFAULT, unless you use the matching OVERRIDE. UPDMAXASSIGNEDVAL YES (IBM option) can refresh the sequence high-water mark after you reload identity values so later INSERT does not collide.
XML columns use field type XML, optionally BINARYXML, with CCSID rules (1208 for character XML is common). LOB columns can be:
DEFINEAUX YES asks LOAD to define auxiliary LOB/XML objects up front. COPYDDN during LOAD copies the base table space only—not LOB, XML, or index spaces. Plan separate COPY jobs for those. Inline statistics likewise skip LOB/XML spaces.
| Option | Meaning |
|---|---|
| DISCARDDN | DD or TEMPLATE for the discard sequential data set (default SYSDISC) |
| DISCARDS n | Maximum discarded source records; 0 means no maximum |
| ERRDDN | Error work data set (default SYSERR) |
| MAPDDN | Maps table-row ids back to input records (default SYSMAP); needed with discards and unique indexes or ENFORCE CONSTRAINTS |
IBM writes discards in the DISCARD phase. Records can be flagged earlier (RELOAD conversion errors, INDEXVAL unique-key errors, ENFORCE RI/check failures) and only then copied from SYSREC to SYSDISC. Therefore SYSDISC must be a sequential BSAM data set with the same RECFM, LRECL, and BLKSIZE as SYSREC. If you omit DISCARDDN, discards are saved only when a SYSDISC DD is present in the JCL (and partition-level INTO TABLE PART DISCARDDN has its own rules).
DISCARDS integer (0 through 2147483647) is a kill switch. DISCARDS 0 means unlimited. If the maximum is reached, LOAD abends and the discard data set is empty—you do not get a partial discard file to inspect. Raise the limit and restart, or TERM UTILITY. BACKOUT YES (with a non-zero DISCARDS cap) can delete rows already loaded in this execution if a discard would leave the object unavailable.
1234567LOAD DATA INDDN SYSREC DISCARDDN SYSDISC DISCARDS 50 ERRDDN SYSERR MAPDDN SYSMAP INTO TABLE HR.EMPLOYEE WHEN (1:3) = 'EMP' (EMPNO POSITION(4:9) CHAR NULLIF(EMPNO=X'000000000000'), LASTNAME POSITION(10:24) CHAR)
Imagine pouring LEGO bricks from a big bin into labeled boxes. WHEN is the rule “only red bricks go in the car box.” Field specs are the sorting tray that says “this slot is the wheel, that slot is the door.” NULLIF is “if this sticker is blank, leave the slot empty.” DEFAULTIF is “if you see GG, put the factory default brick there.” Identity and generated columns are bricks the factory usually stamps for you—OVERRIDE lets you keep the old stamp from a previous build. LOBs are huge posters; you either roll them at the end of the box or write “see folder X.” Discards are the reject bin. If too many bricks are wrong, the factory stops and empties the reject bin so you must fix the limit and try again.
1. What does a LOAD WHEN clause do?
2. NULLIF(ERRORBYTE=' ') means:
3. How do you load a GENERATED ALWAYS identity column from unload data?
4. Where must LOB and XML fields sit in a spanned LOAD record?
5. If DISCARDS n is reached, what happens?