我在SQL Plus中执行select title from book for update of title where isbn = 'A001',然后再打开一个SQL Plus窗口执行update book set price = 20 where isbn = 'A001',更新无法完成,必须把select...for update of 提交了才能执行,请问为什么?这样的话select...for update of 和select...for update有什么区别?

解决方案 »

  1.   

    尝试了一下,发现不同的地方在于出现表连接的时候
    ex: select a.col1 from a, b where a.col1 = b.col2 and ... for udpate 
    这个时候锁住了a,b两个表中满足条件的记录,而
    select a.col1 from a, b where a.col1 = b.col2 and ... for update of a.col1
    仅仅锁住了a表中满足条件的记录所以for update 和for update of 还是有区别的
      

  2.   

    select ...for update of ...
    锁定的是OF后面所写的栏位所在的表
    写一个栏位和多个栏位的意义是一样的
      

  3.   

    主要是楼主你的语句中没有表连接所以for update of 就有点多余了:)个人看法
      

  4.   

    declare
    cursor t_sor is
    select title 
    from book 
    where isbn = 'A001'
    for update of id NOWAIT;
    begin
       for v_sor in t_sor loop
          delete from book WHERE  CURRENT OF t_sor;
       end loop;
    end;以上语句作用于多用户操作同一记录时,防止等待现像.for update of 字段,可用于操作条件.
      

  5.   

    beckhambobo(beckham) 老大给讲讲吧:
    for update of 字段用于操作条件
    不是可以通过where 条件来筛选记录么,而且虽然写了for update of 字段,但是事实上锁住的仍然是整条记录阿,并不是字段,如果其他session需要更新该条记录的其他字段(不含for update of 字段中的字段),仍然是被堵塞的猫猫这块理解的不是特别清楚
      

  6.   

    是的呀我和猫猫的理解一样,现在想到的for update of 的可用性只在有表连接的时候才有有用单表用这种语句不知道会有什么效果?
      

  7.   

    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 a table but not in the b table:create table a
    (id  number,
     name varchar2(10)
    )create table b
    (id  number,
     f_id number
    ) insert into a values(1,'张三');
    insert into b values(1,0);
    commit;declare
        cursor t_sor is
        select a.name
        from   a,b
        where  a.id = b.id
        for update of a.id;
    begin
        for v_sor in t_sor loop
            delete a
            WHERE  CURRENT OF t_sor;
            --delete b
            --WHERE  CURRENT OF t_sor;
        end loop;
    end; you must make sure the row is not changed by another user before the update or delete a table;本人测试过程中,还发现以下错误:
    SQL> declare
      2      cursor t_sor is
      3      select a.name
      4      from   a,b
      5      where  a.id = b.id
      6      for update of a.id;
      7  begin
      8      for v_sor in t_sor loop
      9          delete b
     10          WHERE  CURRENT OF t_sor;
     11      end loop;
     12  end;
     13  /declare
        cursor t_sor is
        select a.name
        from   a,b
        where  a.id = b.id
        for update of a.id;
    begin
        for v_sor in t_sor loop
            delete b
            WHERE  CURRENT OF t_sor;
        end loop;
    end;ORA-01410: invalid ROWID
    ORA-06512: at line 9游标中只对a表进行锁定操,但程序中对b表进行操用,oracle报ORA-01410: invalid ROWID.
      

  8.   

    to beckhambobo(beckham) 老大: 
    where current of cursor_name不是用来表示游标取得的当前的纪录么
    cursor t_sor is
    select a.name
    from a,b
    where a.id = b.id
    for update of a.id;
    游标取得的是a表中的纪录,而delete b WHERE CURRENT OF t_sor;删除的是b表的数据,返回错误也是正常啊猜测where current of 取得游标指针指着的当前纪录的rowid,然后根据rowid进行update,delete之类的操作不知道是否正确,各位老大请帮忙指正
    谢谢,谢谢
      

  9.   

    我最大的不明白就是xiaoxiao1984(笨猫儿)说过的:“for update of 字段用于操作条件
    不是可以通过where 条件来筛选记录么,而且虽然写了for update of 字段,但是事实上锁住的仍然是整条记录阿,并不是字段,如果其他session需要更新该条记录的其他字段(不含for update of 字段中的字段),仍然是被堵塞的”
      

  10.   

    这个偶有点明白了,操作条件不是指操作where 条件,而是在出现表连接的时候指名具体应该索住哪个表的纪录,不需要把所有参与查询的表都锁住
      

  11.   

    简单的说就是,where 条件是用来筛选纪录的, for update of ...是用来筛选表的
      

  12.   

    看来大概就是这样了吧!Oracle这个子句有点歧义,容易让人误解!谢谢各位高手的回答!
      

  13.   

    还不明白,for update of 字段,是针对多表关联情况下,字段是对应表进行锁定,以下例子,同时锁定a,b表行记录.
    SQL> declare
      2      cursor t_sor is
      3      select a.name
      4      from   a,b
      5      where  a.id = b.id
      6      for update of a.id,b.id;
      7  begin
      8      for v_sor in t_sor loop
      9          delete a
     10          WHERE  CURRENT OF t_sor;
     11          delete b
     12          WHERE  CURRENT OF t_sor;
     13      end loop;
     14  end;
     15  /PL/SQL procedure successfully completed
      

  14.   

    那of后加一个和多个字段有什么区别?或指定A表的X字段和指定A表的Y字段有什么区别?如果“字段是对应表进行锁定”那么为什么of后加字段名而不加表名呢?
      

  15.   

    指定a表的x字段或者y字段应该没有太大的区别了
    猜测这样写可以增强程序的易读性吧
      

  16.   

    Oracle的数据封锁最小粒度就是在行级,怎么能封锁到字段级呢?