Update

Update allows you to update a list of database lines defined by a Where condition with values transmitted as parameters.

Syntax

 Update CLASS WHERE_EXPR With ASSIGNMENT_LIST

Examples

 # The active flag is set to 1 (false) for all the customer having CREDIT and DEBIT equal to 0# Return the number of lines updatedFunprog CLEAN_CUSTOMERLocal File CUSTOMER [CUST]Update [CUST] Where DEBIT = 0 and CREDIT = 0 and FLGACT <> 1& With FLGACT = 1End [S]adxuprec# Increment the number of edition done and set the last edit dateUpdate [DIV] Where FLAGEDIT & KEY >= KEY1 With NBEDIT += 1, LASTEDIT=date$# Set the pay for customer to the customer code if not definedUpdate [CUST] Where PAYCUST="" With PAYCUST=CUSTCODE

Description

Update allows you to update columns in a table in one instruction that performs the read, lock, update, and unlock globally. It is much more efficient to use this syntax when several lines have to be updated.

The conditions that can be used are defined in the Where documentation.

Update sets the fstat variable to indicate how the operation worked:

fstat valueDescription
0The update was done successfully, [adxuprec](../4gl/adxuprec.md) records have been updated.
1A lock was found and the transaction has been rolled back.
3At least an update tried to create duplicated keys, so the transaction was rolled back.

Comments

An Update that assigns a column with a column that is updated in the same update is unpredictable. For example:

Update [MYT] With COL1=VALUE1, COL2=COL1+1 gives an unpredictable result for COL2.

The way the locked records are handled may differ from the database implementation:

In both cases, if a lock is detected, the database waits until the lock is released and finally if it is not (deadlock), stops the update and returns with fstat equal to 1.

Update does not modify the content of the [F] class associated with the table.

You cannot use a join abbreviation (set by Link) to perform an update.

The Update must be done inside a transaction, and all the lines are updated. If the transaction implies a great number of lines, it may be that an error 43 (too many locks) is triggered. If this happens, the set-up of the database lock must be changed, or the transaction split in smaller transactions.

Associated errors

Error codeDescription
7Class does not exist (table not opened).
10Data types of expression don't correspond to the data types of the column assigned.
43Too many locks used.

See also

Where, Readlock, File,Rewrite, Trbegin, Commit, Rollback, fstat, adxuprec, Filter.