UPDATE
The UPDATE statement replaces the values of the specified columns with the values of the specified expressions for all rows of the table that satisfy the search condition.
General format
EXEC SQL [ AT Database ] [ FOR Iterations ] UPDATE Options SET { Column-Name = Column-Value } ... [ WHERE { Search-Condition } ] { CURRENT OF Cursor-Name [ FOR ROW Row-Num OF ROWSET ] } [ RETURNING Field ... INTO HostVariable ... ] END-EXEC |
Syntax rules
1. Iterations can be either a host variable or a numeric literal. It specifies the number of rows to be processed.
2. Options is passed to the driver without further checks. Refer to the database documentation for detailed syntax. Syntax errors, if any, are returned at runtime.
5. Field is an alphanumeric literal.
General rules
1. When Search-Condition is specified, only values in rows matching it are replaced.
2. The FOR clause limits the number of times the statement is executed when Search-Condition contains array host variables If you omit this clause,it executes the statement once for each component of the smallest array.
3. When Search-Condition is not specified, values of all rows are replaced.
4. Database identifies the active connection that will execute the query and must be previously defined using a Format 4
DECLARE statement.
5. CURRENT OF allows you to update the last fetched record in the active cursor.
When this syntax is used
o the statement associated to the cursor should not use "SELECT *" and
o the statement associated to the cursor should query from a single table.
Otherwise a "cursor is not updatable" error may occur.
6. FOR ROW n OF ROWSET specifies which row of the current rowset is to be updated. The corresponding row of the rowset is updated, and the cursor remains positioned on the current rowset.
7. The RETURNING clause allows you to receive the updated fields value into host variables.
Examples
Apply a discount to customers meeting a criteria
exec sql update customers set discount = 0.10 where debt_amount < (credit_line * 0.25) end-exec |
Declare, open and fetch a cursor. Update records where the field cust-name is empty.
exec sql declare cust_cur cursor for select cust_code, cust_name from customers end-exec exec sql open cust_cur end-exec perform until 1 = 2 exec sql fetch next cust_cur into :ws-cust-code, :ws-cust-name end-exec if ws-cust-name = space exec sql update customers set cust_name = 'John' where current of cust_cur end-exec end-if if sqlcode = 100 exit perform end-if end-perform exec sql close cust_cur end-exec |