我已经看到很多关于使用rownum或排名分析函数在Oracle中进行分页的答案。但是,我特别想要对这些分页结果应用锁定。问题在于,由于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.
答案 0 :(得分: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.