DarcliGht
DarcliGht

Reputation: 1637

How does statement, 'for update' works?

I am sorry in advance if I sound noob. I am looking through code for stored procedure and I came across:

select 
  ...
into 
  ....
from
  ....
where
  ....
for update;

I don't understand what is the purpose of for update;. I do understand normal update, similar to: http://www.mkyong.com/oracle/oracle-stored-procedure-update-example/. But not able to get my head around for update; and its purpose.

I looked around but could not find clear explanation.

Upvotes: 2

Views: 110

Answers (1)

Rahul Tripathi
Rahul Tripathi

Reputation: 172628

From the document:

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.

Also refer the Oracle docs which says:

The FOR UPDATE clause lets you lock the selected rows so that other users cannot lock or update the rows until you end your transaction. You can specify this clause only in a top-level SELECT statement, not in subqueries.

So the purpose is quite clear it is used when you want to lock your rows during a transaction so that it cannot be used by some other transaction.

You can also refer: FOR UPDATE Clause in a SELECT Statement to get an idea as to how we can use it.

Upvotes: 2

Related Questions