The FOREACH loop to define cursors
FOREACH cursor FOR
SELECT column INTO variable FROM table
. . .
END FOREACH;
FOREACH
EXECUTE FUNCTION name() INTO variable;
END FOREACH;
CREATE_PROCEDURE increase_by_pct( pct INTEGER )
DEFINE s INTEGER;
FOREACH sal_cursor FOR
SELECT salary INTO s FROM employee
WHERE salary > 35000
LET s = s + s * ( pct/100 );
UPDATE employee SET salary = s
WHERE CURRENT OF sal_cursor;
END FOREACH;
END PROCEDURE;
- Declares a cursor
- Selects one salary value at a time from employee
- Increases the salary by a percentage
- Updates employee with the new salary
- Fetches the next salary value
The SELECT statement is placed within a cursor because it returns all the salaries in the table greater than 35000.
The WHERE CURRENT OF clause in the UPDATE statement updates only the row on which the cursor is currently positioned, and sets an update cursor on the current row. An update cursor places an update lock on the row so that no other user can update the row until your update occurs.
BEGIN WORK;
FOREACH sal_cursor FOR
SELECT salary INTO s FROM employee WHERE salary > 35000;
LET s = s + s * ( pct/100 );
UPDATE employee SET salary = s WHERE CURRENT OF sal_cursor
END FOREACH;
COMMIT WORK;
For each iteration of the FOREACH loop in the preceding figure, a new lock is acquired (if you use row level locking). The COMMIT WORK statement releases all of the locks (and commits all of the updated rows as a single transaction) after the last iteration of the FOREACH loop.
CREATE PROCEDURE serial_update();
DEFINE p_col2 INT;
DEFINE i INT;
LET i = 1;
FOREACH cur_su WITH HOLD FOR
SELECT col2 INTO p_col2 FROM customer WHERE 1=1
BEGIN WORK;
UPDATE customer SET customer_num = p_col2 WHERE CURRENT OF cur_su;
COMMIT WORK;
LET i = i + 1;
END FOREACH;
END PROCEDURE;
SPL routine serial_update() commits each row as a separate transaction.