WRITE (Create New Records)

Free-Form Syntax WRITE{(E)} name {data-structure}
Code Factor 1 Factor 2 Result Field Indicators
WRITE (E)   name (file or record format) data-structure _ ER EOF

The WRITE operation writes a new record to a file.

The name operand must be the name of a program-described file or a record format from an externally-described file.

Start of changeIf the data-structure operand is specified, the record is written directly from the data structure to the file. If name refers to a program described file, the data structure is required and can be any data structure of the same length as the file's declared record length. If name refers to a record format from an externally described file, the data structure must be a data structure defined with type EXTNAME(...:*OUTPUT or *ALL) or LIKEREC(...:*OUTPUT or *ALL). For a DISK file, if the data structure is defined with LIKEREC and the type of fields is not specified, the data structure can be used for a WRITE operation if the output buffer layout exactly matches the input buffer layout. See Figure 2 for an example. See File Operations for information on how to define the data structure and how data is transferred between the file and the data structure.End of change

To handle WRITE exceptions (file status codes greater than 1000), either the operation code extender 'E' or an error indicator ER can be specified, but not both. An error occurs if overflow is reached to an externally described print file and no overflow indicator has been specified on the File description specification. For more information on error handling, see File Exception/Errors.

You can specify an indicator in positions 75-76 to signal whether an end of file occurred (subfile is filled) on the WRITE operation. The indicator is set on (an EOF condition) or off every time the WRITE operation is performed. This information can also be obtained from the %EOF built-in function, which returns '1' if an EOF condition occurs and '0' otherwise.

When using the WRITE operation remember:

See Database Null Value Support for information on adding records with null-capable fields containing null values.

For more information, see File Operations.

Figure 1. WRITE Operation with a Program Described File
 *  The WRITE operation writes the fields in the data structure
 *  DS1 to the file, FILE1.
C                   WRITE     FILE1         DS1
Start of change
Figure 2. WRITE Operation for a DISK file with LIKEREC Data Structures

    dcl-ds likerec_default LIKEREC(FMT1);
    dcl-ds likerec_output LIKEREC(FMT1 : *OUTPUT);
    dcl-ds likerec_all LIKEREC(FMT1 : *ALL);

    WRITE FMT1 likerec_default;
    WRITE FMT1 likerec_output;
    WRITE FMT1 likerec_all;
End of change