SQL分页小技巧
时间:2010-10-06 来源:yu_liantao
1.对于SQL2000和SQL2005实用的分页
方法一:
SELECT TOP 页大小 *
FROM table1
WHERE id NOT IN
(
SELECT TOP 页大小*(页数-1) id FROM table1 ORDER BY id
)
ORDER BY id
方法二:
SELECT TOP 页大小 *
FROM table1
WHERE id >
(
SELECT ISNULL(MAX(id),0)
FROM
(
SELECT TOP 页大小*(页数-1) id FROM table1 ORDER BY id
) A
)
ORDER BY id
2.实用与SQL2005
注解:这个方法不适合用于大量数据后面的分页
SELECT TOP 页大小 *
FROM
(
SELECT ROW_NUMBER() OVER (ORDER BY id) AS RowNumber,* FROM table1
) A
WHERE RowNumber > 页大小*(页数-1)
解决办法(参考别人的博客)
select * from table1 where id in (
select id from
(select id, row_number() over (order by id)
scn from table1) t
where scn<100000 and scn>100000-20)
这样就能取出99980到100000之间的数据,依靠id具有的索引读取出来就很快