即使没有rownum,记录也不会返回任何结果

时间:2019-07-17 10:08:30

标签: sql plsql db2

我在DB2中有一条select语句,该语句在执行时返回一条记录,但是,如果我添加类似rownum >=1 and rownum <=2这样的限制,则不会返回任何记录。

选择语句:

当我尝试:

SELECT person.ID, person.SEX, person.BYEAR, person.BMONTH,
        person.BDAY
        FROM MIGRATION.LEGALPERSON person
        JOIN MIGRATION.LEGALPARTNER part ON person.ID = part.ID 
        WHERE person.STATUS = 'ACTIVE'

返回一条记录。

还有

SELECT person.ID, person.SEX, person.BYEAR, person.BMONTH,
            person.BDAY
            FROM MIGRATION.LEGALPERSON person
            JOIN MIGRATION.LEGALPARTNER part ON person.ID = part.ID 
            WHERE person.STATUS = 'ACTIVE' and person.ROWNUM >= 1 and person.ROWNUM <= 2

没有记录返回

能否请我提供有关可能是根本原因的建议。

此致

1 个答案:

答案 0 :(得分:1)

您不能以这种方式使用rownum(请参阅文档https://docs.oracle.com/cd/B19306_01/server.102/b14200/pseudocolumns009.htm):

  

对于ROWNUM值大于正整数的条件测试始终为假

如果您想做类似的事情,请考虑使用row_number分析函数,例如

with temp as
  (select some_column,
          row_number() over (order by date_value) rn
   from your_table
  )
select *
from temp
where rn between 4 and 13

截至ROWNUM本身:

with temp as
  (select some_column,
          rownum rn
   from your_table
   order by date_value
  )
select *
from temp
where rn between 4 and 13