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:
identifier-nis a data element, literal, or data returned from a function call.integer-nis a data element, literal or data returned from a function call that is an integer.statement-nis an imperative statement.
General Rules:
- The file in which record name is described in the
FILE SECTIONmust beOPENwhen theWRITEstatement executes. Record-nameis an 01-level record name defined in a File Description (FD).- The
FROMphrase causes the data inidentifier-1to be copied to record name before the execution of theWRITEstatement.Identifier-1may be in the form of aFUNCTIONcall. - The
ADVANCINGphrase should only be used with files described withORGANIZATION IS LINE SEQUENTIAL. - The
BEFORE ADVANCING integer-1 LINESphrase cau ses record name to be writtenBEFOREintroducing line feeds to the output record. The number of line feeds is described withinteger-1. - The
AFTER ADVANCING integer-1 LINESphrase causes record name to be writtenAFTERintroducing line feeds to the out put record. The number of line feeds is described withinteger-1. - The
BEFORE ADVANCING integer-1 PAGESphrase causes record name to be writtenBEFOREintroducing page feeds to the output record. The number of page feeds is described withinteger-1. - The
AFTER ADVANCING integer-1 PAGESphrase causes record name to be writtenAFTERintroducing page feeds to the output record. The number of page feeds is described withinteger-1. - Files that contain a
LINAGEclause automatically causes lines feeds to be written to the file as stipulated in theLINES AT BOTTOMandLINES AT TOPclauses. - Files that contain a
LINAGEclause and aFOOTINGclause trigger theEND OF PAGEcondition if aWRITEstatement causes any data to be written into theFOOTINGarea, as described in the FD. - If the
END OF PAGEcondition is trapped by anEND OF PAGEphrase, the associatedstatement-listis executed, and then control is passed to the next statement after theWRITEstatement. - The
NOT AT END OF PAGEcondition exists after aWRITEin files that contain aLINAGEclause, and where theWRITEdoes not cause the internal counter maintained by theLINAGEclause to be reached. If theNOT AT END OF PAGEcondition is trapped by aNOT AT END OF PAGEphrase, the associatedstatement-listis executed, and then control is passed to the next statement after theWRITEstatement. - A successful
WRITEstatement to a sequential file causes the data in record name to be written to the end of the file. WRITE'ing aLINE SEQUENTIALrecord to a named pipe is supported.- The
WRITEstatement updates theFILE STATUSvariable.
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:
identifier-nis a data element, literal, or data returned from a function call.statement-nis an imperative statement.
General Rules:
- The file in which record name is described in the
FILE SECTIONmust beOPENwhen theWRITEstatement executes. Record-nameis an01-levelrecord name defined in a File Description (FD).- The
FROMphrase causes the data inidentifier-1to be copied to record name before the execution of theWRITEstatement. - The
WITH LOCKphrase causes the record to beLOCK’ed for the duration of theWRITEstatement. - The
WITH NO LOCKphrase indicates that the record is notLOCK’ed for the duration of theWRITEstatement. - The
INVALID KEYcondition exists if a file-status error is generated by theWRITEstatement. If theINVALID KEYcondition is trapped by anON INVALID KEYphrase, the associatedstatement-listis executed, and then control is passed to the next statement after theWRITEstatement. - Any of the following circumstances triggers the
INVALID KEYcondition:- A
WRITEto a relative file is attempted, and a record with the sameRELATIVE KEYalready exists. - A
WRITEto an indexed file is attempted, and a record with the sameRECORD KEYalready exists. - A
WRITEto an indexed file is attempted, and a record with the sameALTERNATE RECORD KEYalready exists, and theALTERNATE RECORD KEYdoes not allow duplicates.
- A
- If the
INVALID KEYcondition is not trapped by anON INVALID KEYphrase, it can be trapped inDECLARATIVESwith the appropriateUSEstatement. If it is not trapped inDECLARATIVES, it causes the program to abort. - The
NOT INVALID KEYcondition exists after aWRITEstatement is executed successfully. If theNOT INVALID KEYcondition is trapped by aNOT ON INVALID KEYphrase, the associatedstatement-listis executed, and then control is passed to the next statement after theWRITEstatement. - A successful
WRITEstatement 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. - A successful
WRITEstatement to an indexed file causes the index, and data portions of the file to be updated. - The
WRITEstatement updates theFILE STATUSvariable.
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.