使用Select保留顺序..Where In并锁定线路



我在java中准备了以下语句:

with main_select as 
  (select request_id,rownum iden
   from 
    (select request_id
     from queue_requests
     where request_status = 0 and
           date_requested <= sysdate and
           mod(request_id,?) = ?
     order by request_priority desc, oper_id, date_requested)   
   where rownum < ?) 
select *
from queue_requests qr, main_select ms
where qr.request_id in ms.request_id
order by ms.iden for update skip locked;

它不执行:

ORA-02014:无法从DISTINCT、GROUP BY等视图中选择FOR UPDATE。

我将尝试解释为什么我需要所有的select语句:

  • 第一个(内部)选择获得我需要的数据
  • 第二种方法将行数限制为一个数字(我不能将其放在第一个select中,因为oracle首先限制结果,并且只有在对结果进行排序之后,这不是我想要的)
  • 第三个(outside with)select保留了顺序(我尝试使用3个嵌套的select,所以没有with子句,但在这种情况下我找不到保持顺序的方法)。此外,它应该锁定queue_requests表中的行,但由于我从with子句中选择了数据,因此会出现上述错误

因此,我想从queue_requests中选择数据,保留前x行,保留选择的顺序并锁定行。

有办法做到吗?

问题似乎是,您想要对main_select的结果设置锁。我只是猜测,你可以在with子句中的select中做select for update,比如:

with main_select as 
  (select request_id,rownum iden
   from (subselect)
   where rownum < ?
   for update skip locked)

但正如我所说的幸运的猜测。

最新更新