DB2分页查询,结果集大小,ROW_NUMBER OVER

数据库某表中有1533113条数据,但是我想查看最新的几条,输入select * from Table,执行1分多后,返回个内存不足的错误(Out of memory. Partial results returned.)

于是乎,上网找关于db2的分页查询.找到row_number() over()函数和fFETCH FIRST n ROWS ONLY,由于后者是查询前多少行(没有专业书籍,也许有对立语句),所以不使用了.现在介绍前者:

使用方法:

将 ROW_NUMBER() OVER() 子句与谓词一起用于 where 子句中。示例:

SELECT * FROM TABLE
--Out of memory. Partial results returned.
SELECT row_number() over() AS ROW_NUM FROM TABLE WHERE ROW_NUM > 1533013
-- SQL0206N  "ROW_NUM" 在使用它的上下文中无效。  SQLSTATE=42703
SELECT * FROM (SELECT row_number() over() AS ROW_NUM FROM TABLE) WHERE ROW_NUM > 1533013 
-- SQL0104N  在 ")" 后面找到异常标记 ""。预期标记可能包括:"AS"。  SQLSTATE=42601
SELECT * FROM (SELECT row_number() over() AS ROW_NUM FROM TABLE) AS t WHERE ROW_NUM > 1533013 
--ROW_NUM
SELECT * FROM (SELECT row_number() over() AS ROW_NUM,* FROM TABLE) AS t WHERE ROW_NUM > 1533013
-- SQL0104N  在 ") over() AS ROW_NUM," 后面找到异常标记 "*"。预期标记可能包括:""。  SQLSTATE=42601
SELECT * FROM (SELECT row_number() over() AS ROW_NUM,s.* FROM TABLE AS s) AS t  WHERE ROW_NUM > 1533013
终于得到想要的结果集.分享ing
OVER()中间可以带排序方法ORDER BY xxx DESC(ASC) 等等

 

 

你可能感兴趣的:(401DB)