2

我已经看到很多关于在 Oracle 中使用 rownum 或排名分析函数进行分页的答案。但是,我特别想对这些分页结果应用锁定。问题在于,由于 Oracle 中的大多数分页黑客都使用内联视图,因此应用for update是非法的。

那么有没有办法同时执行两者,最好是在同一个 SQL 语句中?

我们现在尝试的一个例子......

select * 
from (
    select ...
    from t
    where ...
    order by ...
)
where rownum <= ? 
for update

我们得到的错误是ORA-02014:, cannot select FOR UPDATE from view with DISTINCT, GROUP BY, etc.

4

1 回答 1

5

这是您可以执行的操作的示例

SQL> create table t as select object_id, owner, object_name, created from all_objects where rownum <= 100;

Table created.

SQL> 
SQL> select *
  2    from t
  3   where rowid in
  4         (select rid
  5            from (select rowid rid, row_number() over(order by object_id) rn from t)
  6           where rn between 11 and 20)
  7     for update;

OBJECT_ID OWNER OBJECT_NAM CREATED
--------- ----- ---------- ---------
       20 SYS   ICOL$      06-MAR-11
       15 SYS   UNDO$      06-MAR-11
       17 SYS   FILE$      06-MAR-11
       13 SYS   UET$       06-MAR-11
       19 SYS   IND$       06-MAR-11
       14 SYS   SEG$       06-MAR-11
       21 SYS   COL$       06-MAR-11
       16 SYS   TS$        06-MAR-11
       12 SYS   FET$       06-MAR-11
       18 SYS   OBJ$       06-MAR-11

10 rows selected.
于 2012-08-22T05:43:17.627 回答