Rewrite

Rewrite allows you to rewrite a database line.

Syntax

 Rewrite CLASS KEY READING_MODE KEY_VALUE

Examples

 # Let's read a line and modify itTrbegin TEST [TEST]Readlock [TEST]KEY2(1) = date$If [S]fstat[TEST]ZONE += 100Rewrite [TEST]CommitElseRollbackEndif# Let's shift lines (line number 2 values are written on line 1, line 3 on line 2...)# Works only if the table is not empty and if no key with no duplicates is presentRead [TEST]KEY1 FirstRead [TEST]KEY1 NextWhile fstat=0Rewrite [TEST]KEY1 PrevRead [TEST]KEY1 NextRead [TEST]KEY1 NextWend

Description

Rewrite allows you to rewrite new values in a line read with one of the various modes:

ModeDescription
FirstThe first line of the cursor defined by the file and filter if applicable.
LastThe last line of the cursor defined by the file and filter if applicable.
NextThe line that follows the current one in the cursor defined by the file and filter if applicable.
PrevThe line that is just before the current one in the cursor defined by the file and filter if applicable.
< valueThe line with the biggest key value that is smaller than the given value.
<= valueThe line with the biggest key value that is smaller or equal to the given value.
> valueThe line with the smallest key value that is bigger than the given value.
>= valueThe line with the smallest key value that is bigger or equal to the given value.
= valueThe first line with the key value equal to the given value.
CurrThe current line (the last that has been read).

The keys that can be used in Rewrite are the following:
* The key defined in an Order By clause if there is one.
* A key that has been defined in the dictionary.

If the key is omitted, the default key (provided by [G]currind) is used.

The fstat variable returns the status of the operation:

ValueDescription
0The rewrite operation succeeded.
1The line is locked.
2With >= or <= mode, the key found was not equal to the value given.
3An attempt of the creation of a duplicate key value was done.
4Begin or end of table reached.
5Line corresponding to the key value not found.

When the Rewrite operation succeeds (for example, when fstat is equal to 0 or 2), the rewritten line becomes the default line.

Comments

The behavior can be different depending on the database:
* In Oracle, the modification done by Rewrite will not be seen until the transaction ends.
* In SQl server, the modification can be seen directly.

You cannot use an abbreviation associated with a join by Link to rewrite a line.

The modification of a line must be done in a transaction.

The rewritten line will be locked until the transaction ends. If the line is already locked, fstat is set to 1.

When several lines have to be updated, use the Update instruction because it performs faster.

Associated errors

Error codeDescription
7Class does not exist (no table opened with the corresponding abbreviation).
8The number of values given, or the number of segments indicated, exceeds the number of segments of the key.
21The key does not exist.
22Incorrect read mode.

See also

File, Readlock, Trbegin, Commit, Rollback, Update, Write, Writeb, Delete, RewriteByKey, DeleteByKey, fstat.