views:

58

answers:

1

I'm selecting from tables FOO and BAR. I'd like to lock the records of FOO which are being returned, but I don't want the records of BAR to be locked.

cursor c_foobar is 
select foo.*, bar.* from
foo, bar
where foo.id = bar.foo_id
for update of <what should I put here?>

It seems like I need to specify individual columns, but I want the entire record of foo to be locked. e.g. I wish I could do something like:

cursor c_foobar is
select foo.*, bar.* from
foo, bar
where foo.id = bar.foo_id
for update of foo

Do I have to enumerate every column of foo in the for update of section in order to lock them all? Or can I arbitrarily choose any column in foo, even those which are not its primary key, and it will lock the entire record?

+5  A: 

From the 10G PL/SQL documentation:

When querying multiple tables, you can use the FOR UPDATE clause to confine row locking to particular tables. Rows in a table are locked only if the FOR UPDATE OF clause refers to a column in that table. For example, the following query locks rows in the employees table but not in the departments table:

DECLARE
  CURSOR c1 IS SELECT last_name, department_name FROM employees, departments
    WHERE employees.department_id = departments.department_id 
          AND job_id = 'SA_MAN'
      FOR UPDATE OF salary;
Tony Andrews
Very good; not sure how I missed that. So I guess it just requires any arbitrary column name from the row... easy but doesn't seem terribly intuitive to me.
RenderIn
I agree, it doesn't. I seem to recall having read somewhere long ago that the requirement to specify columns is so that in some future version perhaps Oracle might only lock particular columns in the row. But my memory is hazy on that.
Tony Andrews
It's a form of self-documenting code, I think: "I only intend to update the column `salary`"
Jeffrey Kemp