在Oracle中选择顶部N,“for update skip locked”

时间:2010-10-15 03:52:22

标签: sql oracle locking top-n

在Oracle中,我可以使用

选择排序表中的前1条消息
select messageid from(
    select 
        messageid, 
        RANK() over (order by messageid asc) as msg_rank 
    from messages
) where msg_rank=1;

正如我在previous question中发现的那样,我可以用

选择一行
select * from messages where rownum < 2 for update skip locked;

但是我无法将这两个概念合并在一起

select messageid from(
    select 
        messageid, 
        RANK() over (order by messageid asc) as msg_rank 
    from messages
) where msg_rank=1 for update skip locked;

-- results in error
-- ORA-02014: cannot select FOR UPDATE from view with DISTINCT, GROUP BY, etc.

如何通过readpast锁定选择前N个?

1 个答案:

答案 0 :(得分:3)

这会有用吗?

select messageid from messages 
    where messageid in (
       select messageid from(
         select 
          messageid, 
          RANK() over (order by messageid asc) as msg_rank 
          from messages
       ) where msg_rank=1
    )
  for update skip locked;