Sub topic 10
Locking and Updating Rows with FOR UPDATE and WHERE CURRENT OF
This chapter assumes that you know basic SELECT and UPDATE statements, PL/SQL blocks, explicit cursors, cursor FOR loops, and basic exception handling.
When a PL/SQL program reads rows and then updates those same rows, another transaction could change them between the read and the update. Oracle provides FOR UPDATE and WHERE CURRENT OF to coordinate this work.
How `FOR UPDATE` locks rows
FOR UPDATE is a clause on a cursor query. It locks the rows returned by the query for the current transaction. Other sessions cannot update or delete those rows until your transaction ends with COMMIT or ROLLBACK.
DECLARE
CURSOR c_emp IS
SELECT employee_id, salary
FROM employees
WHERE department_id = 50
FOR UPDATE OF salary;
BEGIN
FOR r_emp IN c_emp LOOP
DBMS_OUTPUT.PUT_LINE(r_emp.employee_id || ': ' || r_emp.salary);
END LOOP;
ROLLBACK;
END;
/The OF salary phrase identifies the column that the program intends to update. The lock applies to the selected rows, not only to the salary column.
By default, Oracle waits if another session already holds a conflicting lock. Add NOWAIT when waiting is not acceptable:
CURSOR c_emp IS
SELECT employee_id, salary
FROM employees
WHERE department_id = 50
FOR UPDATE OF salary NOWAIT;With NOWAIT, Oracle raises an error immediately if a selected row is locked. SKIP LOCKED is another option. It skips locked rows and returns only rows that are currently available. This is useful for work queues, but it means that the cursor might not process every matching row.
Updating the current row
WHERE CURRENT OF updates the row on which a FOR UPDATE cursor is currently positioned. It avoids repeating the primary-key condition.
DECLARE
CURSOR c_emp IS
SELECT employee_id, salary
FROM employees
WHERE department_id = 50
FOR UPDATE OF salary NOWAIT;
BEGIN
FOR r_emp IN c_emp LOOP
UPDATE employees
SET salary = r_emp.salary * 1.05
WHERE CURRENT OF c_emp;
END LOOP;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
/The cursor FOR loop opens c_emp, fetches each row, positions the cursor on that row, and closes the cursor after the loop. The UPDATE changes the row currently selected by c_emp.
A WHERE CURRENT OF clause requires a cursor declared with FOR UPDATE. The cursor must also be open and positioned on a fetched row. You cannot use it after the cursor is closed or before the first successful fetch.
Transaction boundaries and errors
Locks remain in place until the transaction ends. A COMMIT inside the loop is usually a mistake. It releases the locks and can invalidate the cursor's position, causing errors such as ORA-01002: fetch out of sequence. Commit after all rows have been processed.
Use ROLLBACK in the exception handler when the entire operation must be undone. The RAISE statement sends the original error to the caller after cleanup.
Choosing the pattern
Use one SQL statement when the update can be expressed directly and no per-row PL/SQL work is needed. Use FOR UPDATE with WHERE CURRENT OF when each fetched row needs procedural decisions or calculations. Use NOWAIT for fail-fast behavior and SKIP LOCKED when independent workers can process available rows.
Exercise
Why should a program normally commit after a cursor FOR loop rather than inside it?
Show answer
Committing inside the loop releases row locks and can invalidate the cursor position, causing fetch errors or leaving later rows unprocessed.