Step 1: Defining the cursor
To define a cursor to access the result table, use the DECLARE CURSOR statement.
The DECLARE CURSOR statement names a cursor and specifies a select-statement. The select-statement defines a set of rows that, conceptually, make up the result table. For a serial cursor, the statement looks like this (the FOR UPDATE OF clause is optional):
EXEC SQL DECLARE cursor-nameĀ CURSOR FOR SELECT column-1, column-2 ,... FROM table-name , ... FOR UPDATE OF column-2 ,... END-EXEC.For a scrollable cursor, the statement looks like this (the WHERE clause is optional):
EXEC SQL DECLARE cursor-nameĀ SCROLL CURSOR FOR SELECT column-1, column-2 ,... FROM table-name ,... WHERE column-1 = expression ... END-EXEC.The select-statements shown here are rather simple. However, you can code several other types of clauses in a select-statement within a DECLARE CURSOR statement for a serial and a scrollable cursor.
If you intend to update any columns in any or all of the rows of the identified table (the table named in the FROM clause), include the FOR UPDATE OF clause. It names each column you intend to update. If you do not specify the names of columns, and you specify either the ORDER BY clause or FOR READ ONLY clause, a negative SQLCODE is returned if an update is attempted. If you do not specify the FOR UPDATE OF clause, the FOR READ ONLY clause, the ORDER BY clause, and the result table is not read-only and the cursor is not scrollable, you can update any of the columns of the specified table.
You can update a column of the identified table even though it is not part of the result table. In this case, you do not need to name the column in the SELECT statement. When the cursor retrieves a row (using FETCH) that contains a column value you want to update, you can use UPDATE ... WHERE CURRENT OF to update the row.
For example, assume that each row of the result table includes the EMPNO, LASTNAME, and WORKDEPT columns from the CORPDATA.EMPLOYEE table. If you want to update the JOB column (one of the columns in each row of the CORPDATA.EMPLOYEE table), the DECLARE CURSOR statement should include FOR UPDATE OF JOB ... even though JOB is omitted from the SELECT statement.
The result table and cursor are read-only if any of the following are true:
- The first FROM clause identifies more than one table or view.
- The first FROM clause identifies a read-only view.
- The first FROM clause identifies a user-defined table function.
- The first SELECT clause specifies the keyword DISTINCT.
- The outer subselect contains a GROUP BY clause.
- The outer subselect contains a HAVING clause.
- The first SELECT clause contains a column function.
- The select-statement contains a subquery such that the base object of the outer subselect and of the subquery is the same table.
- The select-statement contains a UNION, UNION ALL, EXCEPT, or INTERSECT operator.
- The select-statement contains an ORDER BY clause, and the SENSITIVE keyword and FOR UPDATE OF clause are not specified.
- The select-statement includes a FOR READ ONLY clause.
- The SCROLL keyword is specified, a FOR UPDATE OF clause is not specified, and the SENSITIVE keyword is not specified.
- The select-list includes a DataLink column and a FOR UPDATE OF clause is not specified.
- The first subselect requires a temporary result table.
- The select-statement includes a FETCH FIRST n ROWS ONLY.
Parent topic:
Examples: Using a cursor