Easytrieve CLOSE Statement

CLOSE terminates access to a file before the Activity section ends. Easytrieve automatically closes all files opened during an activity when that activity terminates—you only need explicit CLOSE when processing phases require releasing resources mid-activity, reopening same file with different intent, closing SQL cursor to start new query, or deleting virtual work files without RETAIN attribute. Syntax is single line CLOSE file-name matching FILE statement name in Library. Next GET, PUT, or READ on that file reopens per Broadcom. Restrictions: cannot CLOSE printer files or automatic input or output files; CLOSE has no effect on IDMS files; virtual files without RETAIN delete when closed. Beginners CLOSE printer wondering why SYSPRINT fails, or omit CLOSE on temp sort work files filling disk. This page teaches syntax, lifecycle with OPEN implied by first I/O, automatic close at activity end, SQL usage, virtual file behavior, valid versus invalid targets, and patterns for multi-phase batch jobs reading same file twice with different logic.

Progress0 of 0 lessons

Statement Format

text
1
CLOSE FILEA

File-name must match name declared in FILE statement. Broadcom example CLOSE FILEA closes that user file. No additional clauses on basic CLOSE grammar.

File Lifecycle in Activity

Open and close behavior
EventBehavior
First I/O on file in activityFile opens implicitly
CLOSE file-nameFile closes; resources released
Next I/O same fileFile reopens automatically
Activity terminationAll opened files auto-close

When Explicit CLOSE Helps

Two-pass processing: first JOB INPUT pass accumulates statistics, CLOSE input, second pass reopens for detail reporting—rare on sequential files because rewind or separate JOB activities often cleaner. SQL programs CLOSE sql-file before new SELECT cursor. Virtual scratch files without RETAIN—CLOSE deletes work dataset freeing space before long REPORT phase. Multi-file orchestration releasing VSAM hold before CALL external program requiring exclusive access.

text
1
2
3
4
5
6
7
8
9
JOB INPUT MASTER PERFORM ACCUM-TOTALS END-JOB CLOSE MASTER JOB INPUT MASTER PERFORM PRINT-DETAIL END-JOB

Illustrative pattern—your site may prefer single JOB with GOTO or separate activities instead; verify rewind support on file type before CLOSE-reopen sequential on tape.

Automatic Close at Activity End

Rely on automatic close for normal batch programs—single pass read, print, STOP. Explicit CLOSE adds line only when mid-activity release required. Missing CLOSE at end is not error; missing STOP is different problem.

Restrictions

  • Cannot CLOSE printer file—SYSPRINT SYSOUT class outputs.
  • Cannot CLOSE automatic input or output files tied to JOB structure.
  • CLOSE has no effect on IDMS files.
  • Virtual file without RETAIN deleted on CLOSE—data lost intentionally.

SQL File CLOSE

SQL Database Processing chapter documents CLOSE on SQL file to terminate cursor so new cursor can be created. Online and batch SQL Easytrieve programs switching queries mid-activity use CLOSE between statements. Coordinate with COMMIT rollback semantics your DBA defines.

CLOSE Versus OPEN Statement

Easytrieve often opens on first I/O without explicit OPEN. OPEN statement exists for cases requiring explicit open before I/O—see OPEN tutorial. CLOSE pairs with explicit lifecycle control; after CLOSE, next I/O reopen replaces need for OPEN in many programs.

Virtual Files and RETAIN

Virtual work files speed sort and intermediate processing. Without RETAIN, CLOSE removes dataset—ensure downstream steps finished. With RETAIN, CLOSE may preserve file for subsequent JOB activity or step per FILE attributes—read FILE statement tutorial for RETAIN semantics on your release.

Error Handling After CLOSE

I/O on closed file before reopen should not occur if logic is sequential—branch mistakes causing GET after CLOSE without intervening reopen path indicate logic bug. File status fields if defined help detect failed I/O after reopen on damaged dataset.

Testing CLOSE Behavior

  1. Confirm virtual work file deletes when CLOSE without RETAIN.
  2. Verify second pass I/O reopens sequential disk file correctly.
  3. SQL: run two queries requiring CLOSE between—confirm no cursor leak.
  4. Attempt CLOSE printer—document compile or runtime rejection.
  5. Activity end without explicit CLOSE still releases DD in JCL spool.

Common CLOSE Mistakes

  • Closing printer or automatic files unnecessarily.
  • CLOSE virtual work file still needed downstream.
  • Assuming CLOSE ends program—need STOP or activity end.
  • Expecting CLOSE on IDMS to work like sequential FILE.
  • Reopen tape file without facility support for rewind.
  • Omitting CLOSE when SQL cursor must reset.

Explain It Like I'm Five

CLOSE is putting a book back on the shelf and closing the cover. You finished that chapter. If you need the book again later, you take it down again—the library opens it on next read. When school ends for the day, the librarian closes all books automatically. Some special notebooks disappear when you close them unless you marked keep on the cover—that is RETAIN.

Exercises

  1. Write CLOSE before second JOB INPUT on same FILE name.
  2. List three file types CLOSE cannot target per Broadcom.
  3. Explain auto-close at activity termination.
  4. Describe SQL CLOSE purpose in two sentences.
  5. Document RETAIN impact on virtual file CLOSE outcome.

Quiz

Test Your Knowledge

1. CLOSE file-name:

  • Terminates access to the file
  • Opens the file
  • Sorts the file
  • Defines Library field

2. At activity termination files opened during activity:

  • Automatically closed
  • Stay open forever
  • Delete JCL
  • Require STOP only

3. CLOSE cannot close:

  • Printer file or automatic input/output files
  • Any user FILE
  • SQL file
  • Indexed file

4. CLOSE on SQL file allows:

  • New cursor creation after close
  • Only batch JOB
  • Report TITLE change
  • MACRO expand

5. Virtual files without RETAIN when closed:

  • Are deleted
  • Promoted to GDG
  • Copied to STEPLIB
  • Unchanged
Published
Read time15 min
AuthorMainframeMaster
Reviewed by MainframeMaster teamVerified: Broadcom Easytrieve Report Generator 11.6 CLOSE statementSources: Broadcom Easytrieve 11.6 Language Reference CLOSE StatementApplies to: Easytrieve CLOSE file lifecycle