FOR UPDATE Clause
Use the FOR UPDATE clause in ESQL/C applications and in DB-Access when you intend to update the values returned by a prepared SELECT statement when the values are fetched.
Element | Description | Restrictions | Syntax |
---|---|---|---|
column | Name of a column that can be updated after a FETCH | Must be in the FROM clause table, but it need not be in the Projection list. All columns must be from the same table. | Identifier |
The FOR UPDATE keywords notify the database server that updating is possible, causing it to use more stringent locking than it would with a Select cursor. You cannot modify data through a cursor without this clause. You can specify which columns can be updated.
EXEC SQL BEGIN DECLARE SECTION; char fname[ 16]; char lname[ 16]; EXEC SQL END DECLARE SECTION; . . . EXEC SQL connect to 'stores_demo'; /* select statement being prepared contains a for update clause */ EXEC SQL prepare x from 'select fname, lname from customer for update'; EXEC SQL declare xc cursor for x; for (;;) { EXEC SQL fetch xc into $fname, $lname; if (strncmp(SQLSTATE, '00', 2) != 0) break; printf("%d %s %s\n",cnum, fname, lname ); if (cnum == 999) --update rows with 999 customer_num EXEC SQL update customer set fname = 'rosey' where current of xc; } EXEC SQL close xc; EXEC SQL disconnect current;
A SELECT . . . FOR UPDATE statement, like an Update cursor, allows you to perform updates that are not possible with the UPDATE statement alone, because both the decision to update and the values of the new data items can be based on the original contents of the row. The UPDATE statement cannot query the table that is being updated.
Restrictions
- The statement can select data from only one table.
- The statement cannot include any aggregate functions.
- The statement cannot include any of the following clauses or keywords: DISTINCT, EXCEPT, FOR READ ONLY, GROUP BY, INTO TEMP, INTERSECT, INTO EXTERNAL, MINUS, ORDER BY, UNION, UNIQUE.
- DECLARE statements that associate a cursor with the statement cannot also include the FOR UPDATE keywords.
- The statement is valid only in ESQL/C routines and (within transactions) in the DB-Access utility. It cannot, for example, be issued within an SPL routine.
For information on how to declare an update cursor for a SELECT statement that does not include a FOR UPDATE clause, see Using the FOR UPDATE Option.
Update cursors in SPL routines
- by declaring a cursor name in the FOREACH statement,
- and then using the WHERE CURRENT OF cursor clause in UPDATE or DELETE statements that operate on the current row of that cursor within the same FOREACH loop.