Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use of FOR UPDATE statement

Tags:

oracle

plsql

I am using PL/SQL (Oracle 11g) to update the EMPLOYEES table salary column.

I have used two separate scripts to do the same thing i.e update the salary of employees.

One script uses FOR UPDATE OF statement where as another script doesn't uses it. In both cases I found that oracle holds the row level locks until we execute the ROLLBACK or COMMIT commands.

Then what is the difference in between two scripts?

Which one is better to use?

Here are the two scripts I am talking about:

-- Script 1: Uses FOR UPDATE OF

declare
cursor cur_emp
is
select employee_id,department_id from employees where department_id = 90 for update of salary;
begin
  for rec in cur_emp
  loop
    update Employees
    set salary = salary*10
    where current of cur_emp;
   end loop;
end;


--Script 2: Does the same thing like script 1 but FOR UPDATE OF is not used here

declare
cursor cur_emp
is
select employee_id,department_id from employees where department_id = 90;
begin
  for rec in cur_emp
  loop
    update Employees
    set salary = salary*10
    where Employee_ID = rec.employee_id;
   end loop;
end;

I found that Oracle acquired the row level locks on both cases. So, what is the benefit of using FOR UPDATE OF and Which is the better way of coding?

like image 974
niceApp Avatar asked Aug 10 '12 02:08

niceApp


People also ask

What is the use of update statement in SQL?

The UPDATE statement in SQL is used to update the data of an existing table in database.

What is the use of for update clause?

FOR UPDATE clause The FOR UPDATE clause is an optional part of a SELECT statement. Cursors are read-only by default. The FOR UPDATE clause specifies that the cursor should be updatable, and enforces a check during compilation that the SELECT statement meets the requirements for an updatable cursor.

How do I use the select for update statement in Oracle?

Oracle / PLSQL: SELECT FOR UPDATE Statement. This Oracle tutorial explains how to use the Oracle/PLSQL SELECT FOR UPDATE statement with syntax and examples. 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.

What is the syntax for updating a table in SQL?

UPDATE Syntax. SET column1 = value1, column2 = value2, ... Note: Be careful when updating records in a table! Notice the WHERE clause in the UPDATE statement. The WHERE clause specifies which record (s) that should be updated. If you omit the WHERE clause, all records in the table will be updated!


1 Answers

When you specify FOR UPDATE, the row is locked at the point that you SELECT the data. Without the FOR UPDATE, the row is locked at the point you UPDATE the row. In the second script, another session could potentially lock the row between the time that the SELECT was executed and the point that you tried to UPDATE it.

If you are dealing with a SELECT statement that returns relatively few rows and a tight inner loop, it is unlikely that there will be an appreciable difference between the two. Adding a FOR UPDATE on the SELECT also gives you the opportunity to add a timeout clause if you don't want your script to block indefinitely if some other session happens to have one of the row you're trying to update locked.

like image 90
Justin Cave Avatar answered Jan 04 '23 12:01

Justin Cave