WRITE WORK FILE

#### Search Results

Search

# WRITE WORK FILE

| `WRITE WORK` [`FILE`] work-file-number [`VARIABLE`] operand1 [(parameter)]

This document covers the following topics:

- WRITE WORK FILE Usage

- WRITE WORK FILE Syntax Description

- External Representation of Fields

- Handling of Large and Dynamic Variables

- WRITE WORK FILE Examples

For an explanation of the symbols used in the syntax diagram, see Syntax Symbols.

Related Statements: `DEFINE WORK FILE` | `READ WORK FILE` | `CLOSE WORK FILE`

Belongs to Function Group: Control of Work Files / PC Files

## WRITE WORK FILE Usage

The `WRITE WORK FILE` statement is used to write records to a physical sequential work file.

It is possible to create a work file in one program or processing loop and to read the same file in a subsequent independent processing loop or in a subsequent program using the `READ WORK FILE` statement.

Note
For Unicode and code page support, see Work Files and Print Files on Windows and Linux Platforms in the Unicode and Code Page Support documentation.

Concurrent writes to a work file opened in append mode are rejected in Natural for Windows: the open operation will fail with error NAT1500 if the work file had been opened by another process.

## WRITE WORK FILE Syntax Description

Operand Definition Table:

Operand | Possible Structure | Possible Formats | Referencing Permitted | Dynamic Definition
--- | --- | --- | --- | ---
`operand1` | C | S | A | G | N | A | U | N | P | I | F | B | D | T | L | C | G |  | yes | no

Note
When using the work file types ENTIRECONNECTION or TRANSFER, operand1 may neither be of format C, nor G.

Syntax Element Description:

Syntax Element | Description
--- | ---
`work-file-number` | Work File Number: The work file number (as defined to Natural) to be used. The work file number is either a numeric constant in the value range 1:32 or a numeric variable of type (B/N/P/I) defined with a `CONST` clause which assigning a value in range (1:32). Variable is a scalar (non-array) without precision digits for type (N/P), length in between 1-4 for type (B), and no redefinition field.
`VARIABLE` | Variable Entry: It is possible to write records with different fields to the same work file with different `WRITE WORK FILE` statements. In this case, the `VARIABLE` entry must be specified in all `WRITE WORK FILE` statements. The records on the external file will be written in variable format. When the operand list includes a dynamic variable (that could change in size for different executions of the `WRITE WORK FILE` statement), the `VARIABLE` entry must be specified in all `WRITE WORK FILE` statements. Variable Index Range: When writing an array to a work file, you can specify a variable index range for the array. For example:
    WRITE WORK FILE work-file-number VARIABLE #ARRAY (I:J)
`operand1` | Fields to Be Written: With `operand1` you specify the fields to be written to the work file. These fields may be database fields, user-defined variables, system variables and/or fields read from another work file using the `READ WORK FILE` statement. An array may be referenced completely or partially to select the occurrences that are to be written to the work file. Group Operands to be Written: A group may be referenced using the group name. All fields belonging to the referenced group will be written to the work file, the sequence is determined by the sequence of the fields in the group. Fields resulting from a redefinition of the referenced group are not written to the work file. If the referenced group is defined as an array, the individual fields of the group are written to the work file as arrays in the definition sequence. For the group definition
    1 GROUP1 (1:3)
        2 FIELD1 (A2)
        2 FIELD2 (A3)
      1 REDEFINE GROUP1
        2 FIELD3 (A15)
the statement
    WRITE WORK FILE 1 GROUP1(*)
is equivalent to
    WRITE WORK FILE 1 GROUP1.FIELD1(*) GROUP1.FIELD2(*)
The statement
    WRITE WORK FILE 1 GROUP1.FIELD3
is equivalent to
    WRITE WORK FILE 1 GROUP1.FIELD1(1) GROUP1.FIELD2(1)
                        GROUP1.FIELD1(2) GROUP1.FIELD2(2)
                        GROUP1.FIELD1(3) GROUP1.FIELD2(3)
`parameter` | Edit Mask Parameters: As `parameter`, you can specify the session parameters `EM` or `EMU`:
`EM=` | Edit Mask: For details on edit masks, see the session parameter `EM` in the Parameter Reference documentation. The `EM` parameter cannot be applied for group operands.
`EMU=` | Unicode Edit Mask: For details on unicode edit masks, see the session parameter `EMU` in the Parameter Reference documentation. The `EMU` parameter cannot be applied for group operands.

## External Representation of Fields

Fields written with a `WRITE WORK FILE` statement are represented in the external file according to their internal definition unless an edit mask is applied.

### Fields without edit masks

For fields written without edit masks, no editing is performed on the field values and they are written in their internal format:

- Format A and B: The number of bytes in the external file is the same as the internal length defined in the Natural program. No editing is performed, and a decimal point is not included in the value.

- Format N: The number of bytes in the external file equals the sum of the internal positions before and after the decimal point. The decimal point is not included in the output.

- Format P: The number of bytes in the external file equals the sum of positions before and after the decimal point, plus 1 for the sign, divided by 2, and rounded up to the next full byte.

Note
No format conversion is performed for fields that are written without edit masks.

Examples of field representations (without edit masks):

Field Definition | Output Record
--- | ---
`#FIELD1 (A10)` | 10 bytes
`#FIELD2 (B15)` | 15 bytes
`#FIELD3 (N1.3)` | 4 bytes
`#FIELD4 (N0.7)` | 7 bytes
`#FIELD5 (P1.2)` | 2 bytes
`#FIELD6 (P6.0)` | 4 bytes

### Fields with edit masks

When an edit mask is applied to a field, the field value is converted using the specified edit mask and the resulting formatted value is written to the work file.

- The internal representation is not used in this case.

- The length of the value written to the file corresponds to the length of the resulting formatted string.

### Special Considerations for System Functions

For the special considerations that apply when `WRITE WORK FILE` is used for the Natural system function `AVER`, `NAVER`, `SUM` or `TOTAL`, see Format/Length Requirements for AVER, NAVER, SUM and TOTAL in the System Functions documentation.

## Handling of Large and Dynamic Variables

Work File Type | Handling
--- | ---
`ASCII` `ASCII-COMPRESSED` | The work file types `ASCII` and `ASCII-COMPRESSED` can handle dynamic and large variables with a maximum field/record length of 32766 bytes.
`SAG` (binary) | The work file type `SAG` (binary) cannot handle dynamic variables and will produce an error. It can, however, handle large variables with a maximum field/record length of 32766 bytes.
`ENTIRECONNECTION` | The work file type `ENTIRECONNECTION` cannot handle dynamic variables. It can, however, handle large variables with a maximum field/record length of 1073741824 bytes.
`PORTABLE` `UNFORMATTED` | Large and dynamic variables can be written into work files or read from work files using the two work file types `PORTABLE` and `UNFORMATTED`. For these types, there is no size restriction for dynamic variables. However, large variables may not exceed a maximum field/record length of 32766 bytes. For the work file type `PORTABLE`, the field information is stored within the work file. The dynamic variables are resized during `READ` if the field size in the record is different from the current size. In the `WRITE WORK FILE` statement, fields are written to the file specified with their byte length. All data types (`DYNAMIC` or not) are treated the same. No structural information is inserted. Note that Natural uses a buffering mechanism, so you can expect the data to be completely written only after a `CLOSE WORK`. This is especially important if the file is to be processed with another utility while Natural is running. With the `READ WORK FILE` statement, fields of fixed length are read with their whole length. If the end-of-file is reached, the remainder of the current field is filled with blanks. The following fields are unchanged. In the case of `DYNAMIC` data types, all the remainder of the file is read unless it exceeds 1073741824 bytes. If the end of file is reached, the remaining fields (variables) are kept unchanged (normal Natural behavior).
`CSV` | The maximum field/record length is 32766 bytes for dynamic and large variables. Dynamic variables are supported. X-arrays are not allowed and will result in an error message.

## WRITE WORK FILE Examples

This section covers the following topics:

- Example 1 - WRITE WORK FILE without Edit Mask

- Example 2 - WRITE WORK FILE with Edit Mask EM and EMU

### Example 1 - WRITE WORK FILE without Edit Mask

    ** Example 'WWFEX1': WRITE WORK FILE
    ************************************************************************
    DEFINE DATA LOCAL
    1 EMPLOY-VIEW VIEW OF EMPLOYEES
      2 PERSONNEL-ID
      2 NAME
    END-DEFINE
    *
    FIND EMPLOY-VIEW WITH CITY = 'LONDON'
      WRITE WORK FILE 1
            PERSONNEL-ID NAME
    END-FIND
    *
    END

### Example 2 - WRITE WORK FILE with Edit Mask EM and EMU

    ** Example 'WWFEX2': WRITE WORK FILE with Edit Mask EM and EMU
    ** Note: It's recommended to define work file 1 with type ASCII/UTF-8
    **       and CP=windows-1252 to handle the Unicode character.
    ************************************************************************
    DEFINE DATA LOCAL
    1 EMPLOY-VIEW VIEW OF EMPLOYEES
      2 PERSONNEL-ID
      2 NAME
      2 CITY
      2 BIRTH
      2 SALARY (1)
    END-DEFINE
    *
    FIND EMPLOY-VIEW WITH CITY = 'MADRID'
      WRITE WORK FILE 1
            PERSONNEL-ID NAME
            BIRTH     (EM=ZD.' 'L(10)' 'YYYY)
            SALARY(1) (EMU=999,999€)
    END-FIND
    *
    END

### On this page

- WRITE WORK FILE Usage
- WRITE WORK FILE Syntax Description
- External Representation of Fields
- Handling of Large and Dynamic Variables
- WRITE WORK FILE Examples