2009-08-09

For Update Option in Select Statement

You can lock a record by querying it, with this FOR UPDATE option in your select statement:

select * from dept where code = '10' for update;

When other users try to query the same record, it will return a resource busy error. It depends on whether you use FOR UPDATE NOWAIT or FOR UPDATE WAIT.

-- immediate return the error
select * from dept
where code = '10'
for update nowait;


-- try for 3 seconds before return the error
select *
from dept
where code = '10'
for update wait 3;

Using FOR UPDATE SKIP LOCKED option can return those records not locked, instead of returning an error message

select *
from dept
for update skip locked;

2 comments:

Anonymous said...

Amiable post and this fill someone in on helped me alot in my college assignement. Say thank you you for your information.

Anonymous said...

Brim over I acquiesce in but I about the brief should prepare more info then it has.