Showing posts with label QSAM. Show all posts
Showing posts with label QSAM. Show all posts

Monday, 28 July 2014

COBOL File I/O Modes: INPUT, OUTPUT, I-O, and EXTEND

A batch job often fails before the first business rule runs because the file was opened in the wrong mode. A program that needs to read customer records should not use OPEN OUTPUT. A program that appends audit records should not recreate the file. In COBOL, the OPEN mode tells the runtime what the program plans to do with the file.

COBOL file I/O modes diagram showing INPUT OUTPUT I-O and EXTEND choices
Choose the mode first.

What are COBOL file I/O modes?

COBOL file I/O modes are the phrases used with the OPEN statement: INPUT, OUTPUT, I-O, and EXTEND. They decide which file statements are valid after the file is open. If the wrong mode is used, later READ, WRITE, REWRITE, or DELETE statements can fail or damage data.

OPEN INPUT  CUSTOMER-FILE
OPEN OUTPUT REPORT-FILE
OPEN I-O    MASTER-FILE
OPEN EXTEND AUDIT-FILE

Good file handling is simple: open the file in the mode that matches the next operation, check FILE STATUS, process the records, and close the file before the program ends.

Quick comparison

OPEN mode Use it when Common valid statements Main risk
INPUT The program only reads an existing file. READ Fails if a required file is missing.
OUTPUT The program creates a new file or replaces old records. WRITE Can clear existing records when used on the wrong file.
I-O The program reads records and updates them. READ, REWRITE, DELETE Can fail when the organization or access mode does not allow updates.
EXTEND The program appends new records after the last record. WRITE Fails for a missing non-optional file and is not valid for every file type.

OPEN INPUT: read an existing file

Use OPEN INPUT when the program reads records and does not change the file. A daily balance report, customer extract reader, or validation job usually opens the input file this way.

SELECT CUSTOMER-FILE ASSIGN TO CUSTIN
    FILE STATUS IS WS-CUST-STATUS.

OPEN INPUT CUSTOMER-FILE
IF WS-CUST-STATUS NOT = "00"
   DISPLAY "CUSTOMER OPEN FAILED: " WS-CUST-STATUS
   GOBACK
END-IF

If the required data set is missing, a typical status is 35. If the file is declared OPTIONAL, the open can succeed with a different status, so support teams should check the program's SELECT clause before assuming the JCL is wrong.

OPEN OUTPUT: create or replace records

Use OPEN OUTPUT for a new output file, such as a report, extract, or unload file. Be careful with existing files. On many jobs, opening an existing file for output means the old contents are replaced by new data.

SELECT REPORT-FILE ASSIGN TO REPTDD
    FILE STATUS IS WS-REPT-STATUS.

OPEN OUTPUT REPORT-FILE
IF WS-REPT-STATUS = "00"
   WRITE REPORT-REC
ELSE
   DISPLAY "REPORT OPEN FAILED: " WS-REPT-STATUS
END-IF

This is the mode that deserves the most review in production changes. A wrong DD name or copied program can point OPEN OUTPUT at a valuable file and replace records that were meant to be kept.

OPEN I-O: read and update records

Use OPEN I-O when the same file is read and changed. A VSAM KSDS maintenance program may read a customer master record, change a field, and then issue REWRITE. A delete program may read a key and then issue DELETE.

OPEN I-O CUSTOMER-MASTER
IF WS-MASTER-STATUS NOT = "00"
   DISPLAY "MASTER OPEN FAILED: " WS-MASTER-STATUS
   GOBACK
END-IF

READ CUSTOMER-MASTER
   INVALID KEY DISPLAY "CUSTOMER NOT FOUND"
END-READ

REWRITE CUSTOMER-MASTER-REC
   INVALID KEY DISPLAY "REWRITE FAILED"
END-REWRITE

I-O is not a shortcut for every file. The file organization, access mode, and data set allocation must support update processing. If a file is sequential and the program only needs to append new rows, EXTEND is often the clearer choice.

OPEN EXTEND: append new records

Use OPEN EXTEND when the program must keep existing records and add new records at the end. Audit logs, transaction history files, and daily append files often use this mode.

OPEN EXTEND AUDIT-FILE
IF WS-AUDIT-STATUS = "00"
   MOVE WS-AUDIT-TEXT TO AUDIT-REC
   WRITE AUDIT-REC
ELSE
   DISPLAY "AUDIT OPEN FAILED: " WS-AUDIT-STATUS
END-IF

If the file may not exist yet, review whether the file should be defined as OPTIONAL. IBM documents different open results for available and unavailable files, so this choice should be deliberate rather than left to a copied file definition.

File status codes to check after OPEN

Every production COBOL program should check FILE STATUS after OPEN. The exact value depends on file organization and runtime behavior, but these statuses appear often in support work.

Status Typical meaning during OPEN First check
00 Open completed normally. Continue processing.
05 Optional file was not available, but open processing continued. Confirm whether SELECT OPTIONAL is intended.
35 Required file was not found or not available. Check the DD statement, catalog entry, and data set name.
39 File attributes do not match the COBOL description. Compare RECFM, LRECL, keys, and record layout.

JCL checks before changing the mode

The COBOL source is only half of the story. The DD statement must point to the right data set and allow the intended action. Before changing an open mode, check the production JCL and scheduler variables.

//CUSTIN   DD DSN=PROD.CUSTOMER.INPUT,DISP=SHR
//REPTDD   DD DSN=PROD.REPORT.DAILY,
//            DISP=(NEW,CATLG,DELETE),
//            SPACE=(CYL,(5,2)),
//            DCB=(RECFM=FB,LRECL=133)

For read-only files, DISP=SHR is common. For new output files, the job often uses DISP=(NEW,CATLG,DELETE). For update jobs, review enqueue rules, restart behavior, and whether another job can read the same data set while updates are running.

Common mistakes

Opening an input file as OUTPUT

This is the dangerous one. A copy-paste change can replace a file that should only be read. Review OPEN OUTPUT statements carefully during code review.

Skipping FILE STATUS after OPEN

If the program does not test the status, the next READ or WRITE may fail far away from the real problem. Log the DD name and status so production support can act quickly.

Using EXTEND when the file is not optional

OPEN EXTEND can fail when a required file is missing. If the first run should create the file, define and test that behavior before the job reaches production.

Practical rule for choosing the mode

  • Use INPUT when the program only reads.
  • Use OUTPUT when the program creates a new result or intentionally replaces old contents.
  • Use I-O when the program reads existing records and updates or deletes them.
  • Use EXTEND when the program keeps existing records and appends new ones.
  • Always check FILE STATUS immediately after the OPEN.

Related Mainframe Forum guides

For the next file-handling topics, read COBOL File Operation, COBOL OPEN Statement, COBOL READ Statement, COBOL File Status, COBOL File Organization, and COBOL FD Entries.

External references

IBM documents these details in the Enterprise COBOL OPEN statement, OPEN statement notes, and opening ESDS, KSDS, and RRDS files pages.

FAQ

Which COBOL open mode is used for reading?

Use OPEN INPUT when the program only needs to read records from an existing file.

Which COBOL open mode appends records?

Use OPEN EXTEND when the program needs to add records after the last existing record.

Can COBOL read and update the same file?

Yes, but the file must be opened with OPEN I-O, and the file organization and access mode must support the update operation.

Why is OPEN OUTPUT risky?

OPEN OUTPUT is risky because it can replace existing records. Use it only when the job is meant to create a fresh output file or clear old contents.

Saturday, 10 August 2013

JCL Spanned Records: RECFM=VBS, LRECL=X, and Safe Usage

A JCL DD statement with DCB=(RECFM=VBS,LRECL=X) tells z/OS that a logical record may be too large to fit in one physical block. The record is still one record to the program, but the access method can store it as segments across more than one block.

JCL spanned records diagram showing one logical record split across two blocks with RECFM VBS and LRECL X
One record can span blocks.

What is a spanned record in JCL?

A spanned record is a variable-length logical record that is stored in pieces when the full record does not fit into a single block. You normally see this on sequential data sets that carry long business records, large print records, XML-like payloads, or converted reports where one logical row can grow past the normal block boundary.

In everyday batch work, most files use FB or VB. A fixed blocked file has the same record length on every row. A variable blocked file can hold records of different sizes, but each logical record still fits inside a block. A spanned variable file, commonly coded as VBS, allows one logical record to continue into the next block when needed.

When should you use RECFM=VBS?

Use RECFM=VBS only when the application has a real need for very long variable records. It is not a default choice for normal transaction files, control files, extract files, or small report feeds. If every record is short enough for a normal VB file, keeping RECFM=VB is simpler and easier for downstream jobs to handle.

Record format Meaning Common use
FB Fixed blocked records Flat files where every record has the same length
VB Variable blocked records Files where records have different lengths but fit inside a block
VBS Variable blocked spanned records Files where one logical record may need more than one block

JCL example for a spanned record data set

The DD statement usually carries the record format in the DCB parameter. Existing shops may also let SMS data classes supply these attributes, so check local standards before hard-coding every value.

//STEP010  EXEC PGM=MYPROG
//OUTFILE  DD  DSN=PROD.REPORT.LONGREC,
//             DISP=(NEW,CATLG,DELETE),
//             SPACE=(CYL,(20,10),RLSE),
//             DCB=(RECFM=VBS,LRECL=X)

The LRECL=X part is used for QSAM when the logical record length can be greater than 32,760 bytes. That is the detail many short notes miss. Without it, someone may copy RECFM=VBS into a job and still fail when the record length is not described correctly for the access method and program.

How RDW, BDW, and segments fit together

Variable records include a record descriptor word, usually called an RDW. Variable blocked records also use a block descriptor word, usually called a BDW, at the start of a block. In a spanned file, z/OS needs extra segment information so it can rebuild the logical record correctly when the record has been split across blocks.

For a COBOL or assembler program, the useful rule is simple: the program should process the logical record, not guess at physical block pieces. If a program reads the file through the normal access method, it should receive the data according to the file definition and program definition. Problems usually appear when a utility, copy program, FTP setup, or downstream reader assumes ordinary VB records.

Why DISP=MOD can be risky with spanned records

The old warning about DISP=MOD is worth keeping. Appending to a spanned-record data set can introduce short-block behavior that surprises later processing. If a job must add more records, the safer pattern is often to create a new output data set and copy or merge the old and new data with a controlled utility step.

//BADAPPND DD DSN=PROD.REPORT.LONGREC,
//            DISP=MOD
//*
//* Better: build a new output generation, then replace by standard process.

That approach also gives operations a cleaner restart point. A failed append can leave the support team asking whether the last logical record was complete, whether the block was closed correctly, and which downstream job consumed the partial file.

Checks before using a spanned record file

Before moving this kind of file into production, check the data set attributes, the program definition, and every job that reads the file. One weak reader in a later step can turn a good file design into a late-night incident.

  • Confirm that the producing program really needs records larger than a normal variable blocked file can handle.
  • Check whether the consumer supports RECFM=VBS, especially sort, copy, transfer, reporting, and unload steps.
  • Use a naming standard or comment that tells support staff why the file is spanned.
  • Avoid casual append processing with DISP=MOD.
  • Test the longest realistic record, not only a small sample file.

Common mistakes

Using VBS for a normal file

Some developers use VBS because it sounds more flexible than VB. That adds a special case where none is needed. If the largest record is comfortably within the normal limit, VB is usually easier for utilities and support teams.

Forgetting the downstream jobs

A file may be created correctly and still fail in a later step. Check any JCL SORT, copy, archive, and transfer process that reads the data set.

Copying only the DCB line

The DCB line is only one part of the design. Space allocation, restart rules, file transfer rules, and program record definitions must match the expected long records.

Related Mainframe Forum guides

For the surrounding JCL basics, read JCL Tutorial: JOB, EXEC and DD Statements, JCL DD Statement, and JCL Data Set Protection. If the output is kept as generations, also review Generation Data Group in JCL.

External references

IBM documents the main record formats, including fixed, variable, blocked, undefined, and spanned formats, in its z/OS record-format material. See IBM: Data set record formats and IBM: Record formats for the formal wording.

FAQ

What does RECFM=VBS mean?

RECFM=VBS means variable blocked spanned records. Records can have different lengths, more than one record can be placed in a block, and a single logical record can continue into another block.

Is RECFM=VBS the same as RECFM=VB?

No. VB handles variable blocked records where each logical record fits in a block. VBS handles variable blocked records that may span blocks.

Why is LRECL=X used with spanned records?

For QSAM, LRECL=X indicates that the logical record length can be greater than 32,760 bytes. Use it only when the program and data set design require that behavior.

Should I use DISP=MOD with spanned records?

Avoid it unless your site has a tested standard for that exact case. Creating a new output data set is usually easier to restart, audit, and support.

New In-feed ads