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.
1CLOSE 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.
| Event | Behavior |
|---|---|
| First I/O on file in activity | File opens implicitly |
| CLOSE file-name | File closes; resources released |
| Next I/O same file | File reopens automatically |
| Activity termination | All opened files auto-close |
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.
123456789JOB 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.
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.
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.
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 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.
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.
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.
1. CLOSE file-name:
2. At activity termination files opened during activity:
3. CLOSE cannot close:
4. CLOSE on SQL file allows:
5. Virtual files without RETAIN when closed: