FILE REWRITE


REWRITE updates a record in a file on a local or remote system. The REWRITE statement should always precede a READ operation with the UPDATE option. The key field value should not change betwen READ and REWRITE.

Syntax -

EXEC CICS REWRITE
     FILE('file-name')
     FROM(data-area)
	 [SYSID(system-name)]
     [LENGTH(length)]
     [RESP(response-field)]
     [RESP2(response-field2)]
     END-EXEC.
  • FILE('file-name') - Specifies the name of the VSAM file where the record is stored.
  • FROM(data-area) - Defines the data area containing the updated record.
  • LENGTH(length) - Specifies the length of the record being rewritten (used for variable-length records).
  • SYSID(system-name) - Specifies the system name to which the request is directed. If SYSID coded and omit RBA, XRBA and RRN, LENGTH and KEYLENGTH must be coded.
  • RBA|RRN|XRBA - Specifies the type of the file and data in the RIDFLD.
  • RESP(response-variable) - Optional. It captures the response code of the REWRITE operation and used to check if the command executed successfully or encountered an error.
  • RESP2(response2-variable) - Optional. It captures the response2 code of the REWRITE operation when the error occured.

How it works?

  • A READ command with UPDATE is issued to retrieve the existing record.
  • The application modifies the record in the working-storage area.
  • The REWRITE command updates the record in the VSAM file.
  • If the update is successful, the transaction continues.
  • If the record is deleted or modified by another task, an error response (NOTFND) is returned.

Short Examples -


Scenario - Reading a Record for Update.

...
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-CUSTOMER-RECORD.
   05 WS-CUST-ID        PIC X(10).
   05 WS-CUST-NAME      PIC X(30).
   05 WS-CUST-ADDRESS   PIC X(50).
   05 WS-CUST-PHONE     PIC X(15).
   
...
PROCEDURE DIVISION.

    MOVE "12345" TO WS-CUST-ID.

    EXEC CICS READ
         FILE('CUSTFILE')
         INTO(WS-CUST-RECORD)
         RIDFLD(WS-CUST-ID)
         UPDATE
         RESP(WS-RESP)
         END-EXEC.

    IF WS-RESP NOT = 0 THEN

        MOVE "9876543210" TO WS-CUST-PHONE

        EXEC CICS REWRITE
             FILE('CUSTFILE')
             FROM(WS-CUST-RECORD)
             RESP(WS-RESP)
             END-EXEC

        IF WS-RESP = 0 THEN
			...
		END-IF
	END-IF.
	
    ...

The READ command is executed with the UPDATE option, meaning the record can be modified. The customer's phone number is updated. The REWRITE command updates the record in the VSAM file.

Error Conditions -


Eror Condition RESP RESP2 Reason
FILENOTFOUND 12 1 A file name in the FILE option is not defined to CICS.
DUPREC 14 150 An attempt is to rewrite a record to a data set whose upgrade set has an alternate index with the UNIQUEKEY attribute, if the corresponding alternate key already exists in the alternate index.
INVREQ 16 30 REWRITE command is issued without a token and no previous READ for UPDATE can be found.
16 46 Attempted to change the length of a BDAM variable length record or block.
16 47 REWRITE instruction includes a token whose value cannot be matched against any token with an existing READ for UPDATE request.
16 55 NOSUSPEND is not allowed because the file is not a VSAM file accessed in RLS mode.
IOERR 17 120 There is an I/O error during the file control operation.
NOSPACE 18 100 No space is available on the direct access device for adding records to a data set.
NOTOPEN 19 60 Below are the reasons for NOTOPEN
  • The requested file is CLOSED and UNENABLED.
  • The requested file is OPEN and in use by other transactions, but a CLOSE request against the file has been received.
  • The requested file is CLOSED and ENABLED, so CICS has tried to open the file as part of executing the request.
ILLOGIC 21 110 VSAM error occurs that is not in one of the other CICS response categories. Any browse that is currently in progress is terminated when this condition is raised.
LENGERR 22 10 The LENGTH option is not specified or a file with variable-length records, or for a BDAM file with undefined format records.
22 12 The length specified exceeds the maximum record size and the record is truncated.
22 14 An incorrect length is specified for a file with fixed-length records.
SYSIDERR 53 130 The SYSID name specified is neither the local region nor a remote system or the link to the remote system is closed.
ISCINVREQ 54 70 The remote system indicates a failure that does not correspond to a known condition.
NOTAUTH 70 101 A resource security check has failed on FILE(filename).
LOCKED 100
An attempt has been made to rewrite a record, but lock exists against the unique alternate key that is involved in the request. AEX8
101 RECORDBUSY 101 107 NOSUSPEND is specified but VSAM holds an active lock against a unique alternate index key which would cause the request to wait. AEX9