Use the FOR UPDATE option to declare an update cursor. You can use the update cursor to modify (update or delete) the current row.
In an ANSI-compliant database, you can use a select cursor to update or delete data if the cursor was not declared with the FOR READ ONLY keywords and it follows the restrictions on update cursors that are described in Subset of SELECT Statement Associated with Cursors. You do not need to use the FOR UPDATE keywords when you declare the cursor.
The following example declares an update cursor:
EXEC SQL declare new_curs cursor for select * from customer_notansi for update;
In an update cursor, you can update or delete rows in the active set. After you create an update cursor, you can update or delete the currently selected row by using an UPDATE or DELETE statement with the WHERE CURRENT OF clause. The words CURRENT OF refer to the row that was most recently fetched; they take the place of the usual test expressions in the WHERE clause.
An update cursor lets you perform updates that are not possible with the UPDATE statement because the decision to update and the values of the new data items can be based on the original contents of the row. Your program can evaluate or manipulate the selected data before it decides whether to update. The UPDATE statement cannot interrogate the table that is being updated.
You can specify particular columns that can be updated. The columns need not appear in the Select list of the Projection clause.
When you declare an update cursor, you can limit the update to specific columns by including the OF keyword and a list of columns. You can modify only those named columns in subsequent UPDATE statements. The columns need not be in the select list of the SELECT clause.
The next example declares an update cursor and specifies that this cursor can update only the fname and lname columns in the customer_notansi table:
EXEC SQL declare name_curs cursor for select * from customer_notansi for update of fname, lname;
By default, unless declared as FOR READ ONLY, a select cursor in a database that is ANSI compliant is an update cursor, so the FOR UPDATE keywords are optional. If you want an update cursor to be able to modify only some of the columns in a table, however, you must specify these columns in the FOR UPDATE OF column list.
The principal advantage to specifying columns is documentation and preventing programming errors. (The database server refuses to update any other columns.) An additional advantage is improved performance, when the SELECT statement meets the following criteria:
If the columns that you intend to update are part of the index that is used to process the SELECT statement, the database server keeps a list of each updated row, to ensure that no row is updated twice. If the OF keyword specifies which columns can be updated, the database server determines whether or not to keep the list of updated rows. If the database server determines that the work of keeping the list is unnecessary, performance improves. If you do not use the OF column list, the database server always maintains a list of updated rows, although the list might be unnecessary.
The following example contains ESQL/C code that uses an update cursor with a DELETE statement to delete the current row.
Whenever the row is deleted, the cursor remains between rows. After you delete data, you must use a FETCH statement to advance the cursor to the next row before you can refer to the cursor in a DELETE or UPDATE statement.
EXEC SQL declare q_curs cursor for select * from customer where lname matches :last_name for update; EXEC SQL open q_curs; for (;;) { EXEC SQL fetch q_curs into :cust_rec; if (strncmp(SQLSTATE, "00", 2) != 0) break; /* Display customer values and prompt for answer */ printf("\n%s %s", cust_rec.fname, cust_rec.lname); printf("\nDelete this customer? "); scanf("%s", answer); if (answer[0] == 'y') EXEC SQL delete from customer where current of q_curs; if (strncmp(SQLSTATE, "00", 2) != 0) break; } printf("\n"); EXEC SQL close q_curs;
The FOR UPDATE keywords notify the database server that updating is possible and cause it to use more stringent locking than with a select cursor. You declare an update cursor to let the database server know that the program might update (or delete) any row that it fetches as part of the SELECT statement. The update cursor employs promotable locks for rows that the program fetches. Other programs can read the locked row, but no other program can place a promotable lock (also called a write lock). Before the program modifies the row, the row lock is promoted to an exclusive lock.
It is possible to declare an update cursor with the WITH HOLD keywords, but the only reason to do so is to break a long series of updates into smaller transactions. You must fetch and update a particular row in the same transaction.
If an operation involves fetching and updating a large number of rows, the lock table that the database server maintains can overflow. The usual way to prevent this overflow is to lock the entire table that is being updated. If this action is impossible, an alternative is to update through a hold cursor and to execute COMMIT WORK at frequent intervals. You must plan such an application carefully, however, because COMMIT WORK releases all locks, even those that are placed through a hold cursor.
Enterprise Edition Home | Express Edition Home | [ Top of Page | Previous Page | Next Page | Contents | Index ]