Previous Topic: Format SYSOUT Output

Next Topic: Delete Records from a UDB

Update Records in a UDB

The &FILE PUT statement is also used to update records in a UDB. A record can either be updated by first reading it with an &FILE GET statement and then replacing it with an &FILE PUT statement or by replacing it directly with an &FILE PUT statement. The method chosen will be determined by the application. Regardless of the method to be utilized the key of the record is identified using the KEY= or KEYVAR= operand on the &FILE statement.

If the key was specified on the &FILE GET statement, there is no need to specify it again on the &FILE PUT statement because the retrieved key remains current for the file.

If the possibility exists that multiple updates for the same record can be attempted concurrently, precautions must be taken to ensure that updates are not lost or inadvertently overwritten.

The success of the &FILE PUT statement can be tested using the &FILERC system variable. If the &FILE PUT statement is not successful the &VSAMFDBK system variable contains a standard VSAM completion code indicating the type of error that occurred. For example:

&FILE OPEN ID=MYFILE FORMAT=DELIMITED
&FILE GET ID=MYFILE KEY='RECORD1' VARS=B*
&IF &FILERC NE 0 &GOTO .NORECORD
     .
     .
     .  update data
     .
     .
&FILE PUT ID=MYFILE ARGS RANGE=(1,7)
&IF &FILERC NE 0 &WRITE DATA=ERROR CODE=&VSAMFDBK

&FILE PUT will replace a record which already exists. Where multiple concurrent updates are possible the NCL procedure must ensure that it has exclusive control of a record prior to issuing the &FILE PUT statement to update it. This can be achieved by first reading the record with an &FILE GET statement that specifies the OPT=UPD operand. This operand indicates that exclusive control of the record is required. If the record is already in use, the &FILE GET statement fails with &FILERC set to 8 and &VSAMFDBK set to 14. The NCL procedure can then take alternative action, such as delaying processing for a short period before retrying the &FILE GET. Having obtained the record the &FILE PUT statement can be issued to complete the update.

When using &FILE GET with the UPD option, the NCL procedure should complete processing of the record as quickly as possible. It is not good practice to obtain exclusive control of a record and then issue a full-screen panel which waits for operator input. This can delay other users for excessive periods.

An example of a procedure using the &FILE GET ... OPT=UPD operand follows. This example issues a one second delay if exclusive control of the record cannot be obtained, and then retries the &FILE GET.

&FILE OPEN ID=MYFILE FORMAT=DELIMITED
.RETRY
   &FILE GET ID=MYFILE KEY='RECORD1' OPT=UPD ARGS
   &IF &FILERC EQ 0 &THEN &GOTO .UPDATE
   &IF &FILERC EQ 8 AND &VSAMFDBK EQ 14 &THEN &GOTO .WAIT
   &ENDAFTER &WRITE DATA=ERROR CODE=&VSAMFDBK
.WAIT
   &DELAY 1
   &GOTO .RETRY
.UPDATE
   .
   .
   .  update data
   .
   .
&FILE PUT ID=MYFILE ARGS RANGE=(1,7)
&IF &FILERC NE 0 &THEN &WRITE DATA=ERROR CODE=&VSAMFDBK