The WRITE Statement
Syntax
Format 1
WRITE record-name-1 [ FROM { identifier-1
literal-1 }]
[ { BEFORE
AFTER } ADVANCING { { identifier-2
integer-1 }[ LINE
LINES ] } ]
{ mnemonic-name-1
PAGE }
[AT { END-OF-PAGE
EOP } imperative-statement-1]
[NOT AT { END-OF-PAGE
EOP } imperative-statement-1]
[END-WRITE]
Format 2
WRITE record-name-1 [ FROM { identifier-1
literal-1 }]
[INVALID KEY imperative-statement-1]
[NOT INVALID KEY imperative-statement-2]
[END-WRITE]
Description
The WRITE statement is used to add records to a file. The record is added to the file associated with record-name-1. If the FROM clause is included, the FROM item is copied into record-name-1 before the data record is written. Each of the two formats works slightly differently, and is described below in its correspondingly numbered item.
1. The first format performs a sequential write of the file.
If that file is a printer file, the ADVANCING clause may be used to position the lines on the printed page. BEFORE ADVANCING cause the positioning to occur after the line is written on the page. AFTER ADVANCING caused the positioning to occur before the line is written on the page. The LINES clause is used to determine how many lines down the page the paper is to be positioned. The number of lines advanced includes the line to be written. The PAGE phrase causes advancing to the top of the next page. If the file is a printer file and no ADVANCING clause is specified, the paper will be advanced 1 line before the line is printed.
For printer files where the LINAGE clause is specified, if the write causes the allowed number of lines per page to be exceeded, the code between AT END-OF-PAGE and NOT AT END-OF-PAGE, if any, will be executed. If the write does not cause the allowed number of lines per page to be exceeded, the code between NOT AT END-OF-PAGE and END-WRITE, if any, will be executed.
2. The second format performs a random write of the file. If the record key matches a key already in the file and WITH NO DUPLICATES has been specified on that key, no record is written and the code between INVALID KEY and NOT INVALID KEY, if any, will be executed. If the write is successful, the code between NOT INVALID KEY and END-WRITE, if any, will be executed.
Tips
1. If there is a file status associated with the file, it will be set after the WRITE is executed to indicate the result of the WRITE. A value of "00" means successful and "23" means there was a duplicate key in the file prior to the execution of the WRITE.
2. Any file mentioned must be the subject of a SELECT statement and a file description (FD). Files must be open for input/output or output prior to executing the WRITE statement.
3. When used in a program with a SORT statement, the sort record itself can never be written.
4. Be sure to fill in the values of the data fields before executing the WRITE statement.
5. Mixing BEFORE and AFTER advancing WRITEs can result in overwriting previously written lines if not done with extreme care.
6. You can underline text on a page by defining a separate line of underline characters and writing it after advancing 0 lines.
7. In order to invoke the AT END-OF-PAGE feature, the LINAGE clause must be included in the file description (FD) for the printer file.
Syntax
Format 1
WRITE record-name-1 [ FROM { identifier-1
literal-1 }]
[ { BEFORE
AFTER } ADVANCING { { identifier-2
integer-1 }[ LINE
LINES ] } ]
{ mnemonic-name-1
PAGE }
[AT { END-OF-PAGE
EOP } imperative-statement-1]
[NOT AT { END-OF-PAGE
EOP } imperative-statement-1]
[END-WRITE]
Format 2
WRITE record-name-1 [ FROM { identifier-1
literal-1 }]
[INVALID KEY imperative-statement-1]
[NOT INVALID KEY imperative-statement-2]
[END-WRITE]
Description
The WRITE statement is used to add records to a file. The record is added to the file associated with record-name-1. If the FROM clause is included, the FROM item is copied into record-name-1 before the data record is written. Each of the two formats works slightly differently, and is described below in its correspondingly numbered item.
1. The first format performs a sequential write of the file.
If that file is a printer file, the ADVANCING clause may be used to position the lines on the printed page. BEFORE ADVANCING cause the positioning to occur after the line is written on the page. AFTER ADVANCING caused the positioning to occur before the line is written on the page. The LINES clause is used to determine how many lines down the page the paper is to be positioned. The number of lines advanced includes the line to be written. The PAGE phrase causes advancing to the top of the next page. If the file is a printer file and no ADVANCING clause is specified, the paper will be advanced 1 line before the line is printed.
For printer files where the LINAGE clause is specified, if the write causes the allowed number of lines per page to be exceeded, the code between AT END-OF-PAGE and NOT AT END-OF-PAGE, if any, will be executed. If the write does not cause the allowed number of lines per page to be exceeded, the code between NOT AT END-OF-PAGE and END-WRITE, if any, will be executed.
2. The second format performs a random write of the file. If the record key matches a key already in the file and WITH NO DUPLICATES has been specified on that key, no record is written and the code between INVALID KEY and NOT INVALID KEY, if any, will be executed. If the write is successful, the code between NOT INVALID KEY and END-WRITE, if any, will be executed.
Tips
1. If there is a file status associated with the file, it will be set after the WRITE is executed to indicate the result of the WRITE. A value of "00" means successful and "23" means there was a duplicate key in the file prior to the execution of the WRITE.
2. Any file mentioned must be the subject of a SELECT statement and a file description (FD). Files must be open for input/output or output prior to executing the WRITE statement.
3. When used in a program with a SORT statement, the sort record itself can never be written.
4. Be sure to fill in the values of the data fields before executing the WRITE statement.
5. Mixing BEFORE and AFTER advancing WRITEs can result in overwriting previously written lines if not done with extreme care.
6. You can underline text on a page by defining a separate line of underline characters and writing it after advancing 0 lines.
7. In order to invoke the AT END-OF-PAGE feature, the LINAGE clause must be included in the file description (FD) for the printer file.