一尘不染

在Oracle中选择“或跳过更新锁定”的前N个

sql

在Oracle中,我可以使用以下命令选择排序表中的前1条消息:

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

正如我在上一个问题中发现的那样,我可以选择一行

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.

如何选择前行锁定的前N个?


阅读 139

收藏
2021-05-23

共1个答案

一尘不染

这会工作吗?

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;
2021-05-23