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-n
is a data element, literal, or data returned from a function call.integer-n
is a data element, literal or data returned from a function call that is an integer.statement-n
is an imperative statement.
General Rules:
- The file in which record name is described in the
FILE SECTION
must beOPEN
when theWRITE
statement executes. Record-name
is an 01-level record name defined in a File Description (FD).- The
FROM
phrase causes the data inidentifier-1
to be copied to record name before the execution of theWRITE
statement.Identifier-1
may be in the form of aFUNCTION
call. - The
ADVANCING
phrase should only be used with files described withORGANIZATION IS LINE SEQUENTIAL
. - The
BEFORE ADVANCING integer-1 LINES
phrase cau ses record name to be writtenBEFORE
introducing line feeds to the output record. The number of line feeds is described withinteger-1
. - The
AFTER ADVANCING integer-1 LINES
phrase causes record name to be writtenAFTER
introducing line feeds to the out put record. The number of line feeds is described withinteger-1
. - The
BEFORE ADVANCING integer-1 PAGES
phrase causes record name to be writtenBEFORE
introducing page feeds to the output record. The number of page feeds is described withinteger-1
. - The
AFTER ADVANCING integer-1 PAGES
phrase causes record name to be writtenAFTER
introducing page feeds to the output record. The number of page feeds is described withinteger-1
. - Files that contain a
LINAGE
clause automatically causes lines feeds to be written to the file as stipulated in theLINES AT BOTTOM
andLINES AT TOP
clauses. - Files that contain a
LINAGE
clause and aFOOTING
clause trigger theEND OF PAGE
condition if aWRITE
statement causes any data to be written into theFOOTING
area, as described in the FD. - If the
END OF PAGE
condition is trapped by anEND OF PAGE
phrase, the associatedstatement-list
is executed, and then control is passed to the next statement after theWRITE
statement. - The
NOT AT END OF PAGE
condition exists after aWRITE
in files that contain aLINAGE
clause, and where theWRITE
does not cause the internal counter maintained by theLINAGE
clause to be reached. If theNOT AT END OF PAGE
condition is trapped by aNOT AT END OF PAGE
phrase, the associatedstatement-list
is executed, and then control is passed to the next statement after theWRITE
statement. - A successful
WRITE
statement to a sequential file causes the data in record name to be written to the end of the file. WRITE
'ing aLINE SEQUENTIAL
record to a named pipe is supported.- The
WRITE
statement updates theFILE 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:
identifier-n
is a data element, literal, or data returned from a function call.statement-n
is an imperative statement.
General Rules:
- The file in which record name is described in the
FILE SECTION
must beOPEN
when theWRITE
statement executes. Record-name
is an01-level
record name defined in a File Description (FD).- The
FROM
phrase causes the data inidentifier-1
to be copied to record name before the execution of theWRITE
statement. - The
WITH LOCK
phrase causes the record to beLOCK
’ed for the duration of theWRITE
statement. - The
WITH NO LOCK
phrase indicates that the record is notLOCK
’ed for the duration of theWRITE
statement. - The
INVALID KEY
condition exists if a file-status error is generated by theWRITE
statement. If theINVALID KEY
condition is trapped by anON INVALID KEY
phrase, the associatedstatement-list
is executed, and then control is passed to the next statement after theWRITE
statement. - Any of the following circumstances triggers the
INVALID KEY
condition:- A
WRITE
to a relative file is attempted, and a record with the sameRELATIVE KEY
already exists. - A
WRITE
to an indexed file is attempted, and a record with the sameRECORD KEY
already exists. - A
WRITE
to an indexed file is attempted, and a record with the sameALTERNATE RECORD KEY
already exists, and theALTERNATE RECORD KEY
does not allow duplicates.
- A
- If the
INVALID KEY
condition is not trapped by anON INVALID KEY
phrase, it can be trapped inDECLARATIVES
with the appropriateUSE
statement. If it is not trapped inDECLARATIVES
, it causes the program to abort. - The
NOT INVALID KEY
condition exists after aWRITE
statement is executed successfully. If theNOT INVALID KEY
condition is trapped by aNOT ON INVALID KEY
phrase, the associatedstatement-list
is executed, and then control is passed to the next statement after theWRITE
statement. - 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. - A successful
WRITE
statement to an indexed file causes the index, and data portions of the file to be updated. - The
WRITE
statement updates theFILE 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.