Skip to content

WRITE Statement

The WRITE statement adds a record to a data file.

Format 1

The Format 1 WRITE statement writes a record to a sequential file, and includes language for writing to a PRINT file, which is a special form of a line sequential file.

       WRITE record-name [ FROM identifier-1 ] 
              [ {BEFORE} ADVANCING { integer-1 [LINE           ] } ] 
                {AFTER }                       [LINES          ] 
                                               [PAGE           ] 
                                               [ mnemonic-name ] 
              [ AT {END-OF-PAGE} statement-1 ] 
                   {EOP } 
              [ NOT AT {END-OF-PAGE} statement-2 ] 
                       {EOP } 
              [ END-WRITE ]

Syntax:

  1. identifier-n is a data element, literal, or data returned from a function call.
  2. integer-n is a data element, literal or data returned from a function call that is an integer.
  3. statement-n is an imperative statement.

General Rules:

  1. The file in which record name is described in the FILE SECTION must be OPEN when the WRITE statement executes.
  2. Record-name is an 01-level record name defined in a File Description (FD).
  3. The FROM phrase causes the data in identifier-1 to be copied to record name before the execution of the WRITE statement. Identifier-1 may be in the form of a FUNCTION call.
  4. The ADVANCING phrase should only be used with files described with ORGANIZATION IS LINE SEQUENTIAL.
  5. The BEFORE ADVANCING integer-1 LINES phrase cau ses record name to be written BEFORE introducing line feeds to the output record. The number of line feeds is described with integer-1.
  6. The AFTER ADVANCING integer-1 LINES phrase causes record name to be written AFTER introducing line feeds to the out put record. The number of line feeds is described with integer-1.
  7. The BEFORE ADVANCING integer-1 PAGES phrase causes record name to be written BEFORE introducing page feeds to the output record. The number of page feeds is described with integer-1.
  8. The AFTER ADVANCING integer-1 PAGES phrase causes record name to be written AFTER introducing page feeds to the output record. The number of page feeds is described with integer-1.
  9. Files that contain a LINAGE clause automatically causes lines feeds to be written to the file as stipulated in the LINES AT BOTTOM and LINES AT TOP clauses.
  10. Files that contain a LINAGE clause and a FOOTING clause trigger the END OF PAGE condition if a WRITE statement causes any data to be written into the FOOTING area, as described in the FD.
  11. If the END OF PAGE condition is trapped by an END OF PAGE phrase, the associated statement-list is executed, and then control is passed to the next statement after the WRITE statement.
  12. The NOT AT END OF PAGE condition exists after a WRITE in files that contain a LINAGE clause, and where the WRITE does not cause the internal counter maintained by the LINAGE clause to be reached. If the NOT AT END OF PAGE condition is trapped by a NOT AT END OF PAGE phrase, the associated statement-list is executed, and then control is passed to the next statement after the WRITE statement.
  13. A successful WRITE statement to a sequential file causes the data in record name to be written to the end of the file.
  14. WRITE'ing a LINE SEQUENTIAL record to a named pipe is supported.
  15. The WRITE statement updates the FILE STATUS variable.

Format 2

The Format 2 WRITE statement writes a record to an indexed file, or a relative file, and includes language for trapping the INVALID KEY condition.

       WRITE record-name [ FROM identifier-1 ] 
              [ WITH [NO] LOCK ] 
              [ INVALID KEY statement-1 ] 
              [ NOT INVALID KEY statement-2 ] 
              [ END-WRITE ]

Syntax:

  1. identifier-n is a data element, literal, or data returned from a function call.
  2. statement-n is an imperative statement.

General Rules:

  1. The file in which record name is described in the FILE SECTION must be OPEN when the WRITE statement executes.
  2. Record-name is an 01-level record name defined in a File Description (FD).
  3. The FROM phrase causes the data in identifier-1 to be copied to record name before the execution of the WRITE statement.
  4. The WITH LOCK phrase causes the record to be LOCK’ed for the duration of the WRITE statement.
  5. The WITH NO LOCK phrase indicates that the record is not LOCK’ed for the duration of the WRITE statement.
  6. The INVALID KEY condition exists if a file-status error is generated by the WRITE statement. If the INVALID KEY condition is trapped by an ON INVALID KEY phrase, the associated statement-list is executed, and then control is passed to the next statement after the WRITE statement.
  7. Any of the following circumstances triggers the INVALID KEY condition:
    • A WRITE to a relative file is attempted, and a record with the same RELATIVE KEY already exists.
    • A WRITE to an indexed file is attempted, and a record with the same RECORD KEY already exists.
    • A WRITE to an indexed file is attempted, and a record with the same ALTERNATE RECORD KEY already exists, and the ALTERNATE RECORD KEY does not allow duplicates.
  8. If the INVALID KEY condition is not trapped by an ON INVALID KEY phrase, it can be trapped in DECLARATIVES with the appropriate USE statement. If it is not trapped in DECLARATIVES, it causes the program to abort.
  9. The NOT INVALID KEY condition exists after a WRITE statement is executed successfully. If the NOT INVALID KEY condition is trapped by a NOT ON INVALID KEY phrase, the associated statement-list is executed, and then control is passed to the next statement after the WRITE statement.
  10. A successful WRITE statement to a relative file causes the data in record name to be written to the position in the relative file described by the data item containing the relative key.
  11. A successful WRITE statement to an indexed file causes the index, and data portions of the file to be updated.
  12. The WRITE statement updates the FILE STATUS variable.

Code Sample:

       ... 
              SELECT PRINT-FILE-1 ASSIGN TO "PRINTER". 

              SELECT IDX-FILE-1 ASSIGN TO "IDX-FILE-1" 
              ORGANIZATION IS INDEXED 
              ACCESS IS DYNAMIC 
              RECORD KEY IS IDX-KEY 
              FILE STATUS IS FILE-1-STAT. 
       ... 
       FD PRINT-FILE-1. 
       01 PRINT-RECORD PIC X(60). 

       FDFILE-1. 
       01 FILE-1-RECORD. 
              03 FILE-KEY PIC X(10). 
       ... 
       77 FILE-1-STAT PIC XX. 
       01 WS-IDX-FILE PIC X(10). 
       ... 
              WRITE PRINT-RECORD BEFORE ADVANCING 1 LINE. 

              WRITE PRINT-RECORD AFTER ADVANCING 1 LINE. 

              WRITE PRINT-RECORD AFTER ADVANCING 2 LINES 
                     AT END-OF-PAGE CONTINUE 
                     NOT AT END-OF-PAGE CONTINUE 
              END-WRITE. 

              WRITE PRINT-RECORD AFTER ADVANCING PAGE. 

              WRITE PRINT-RECORD BEFORE ADVANCING PAGE.

              WRITE FILE-1-RECORD. 

              WRITE FILE-1-RECORD 
                     INVALID KEY 
                            DISPLAY "INVALID KEY!" LINE 10 COL 10 
                     NOT INVALID KEY 
                            CONTINUE 
              END-WRITE. 

              WRITE FILE-1-RECORD FROM WS-IDX-FILE.
Back to top