SELECT FOR UPDATE Statement

Course- Oracle/PLSQL >

This Oracle tutorial explains how to use the Oracle/PLSQL SELECT FOR UPDATE statement with syntax and examples.

Description

The SELECT FOR UPDATE statement allows you to lock the records in the cursor result set. You are not required to make changes to the records in order to use this statement. The record locks are released when the next commit or rollback statement is issued.

Syntax

The syntax for the SELECT FOR UPDATE statement in Oracle/PLSQL is:

CURSOR cursor_name

IS

   select_statement

   FOR UPDATE [OF column_list] [NOWAIT];

Parameters or Arguments

cursor_name

The name of the cursor.

select_statement

A SELECT statement that will populate your cursor result set.

column_list

The columns in the cursor result set that you wish to update.

NOWAIT

Optional. The cursor does not wait for resources.

Example

For example, you could use the SELECT FOR UPDATE statement as follows:

CURSOR c1

IS

  SELECT course_number, instructor

  FROM courses_tbl

  FOR UPDATE OF instructor;

If you plan on updating or deleting records that have been referenced by a SELECT FOR UPDATE statement, you can use the WHERE CURRENT OF statement.