sql 分部分从sql server数据库表中获取记录

2021-09-10 00:00:00 sql tsql sql-server-2008 sql-server

我想从我的 sql server 数据库中的表中分部分获取记录.比如,在一个查询中我想查看前 1000 条记录,在下一个查询中查看下 1000 条记录.同样..

I want to fetch records from a table in my sql server database in parts. Like, in one query I want to see first 1000 records, in next query next 1000 records. Likewise..

可以使用 sql server 吗?我正在使用 sql server 2008.在谷歌搜索时,我发现 mysqlLIMIT 子句,但它不适用于 sql server.任何人都可以在 Sql 中给出.请帮忙.

Is it possible with sql server ? I am using sql server 2008. While googling, I found LIMIT clause for mysql, but it does not work for sql server. So can any one give in Sql. Please help.

推荐答案

前 1000 条记录:

First 1000 records:

SELECT  TOP 1000 *
FROM    mytable
ORDER BY
        mycolumn

通用解决方案(支持偏移)

General solution (supports offset)

SELECT  *
FROM    (
        SELECT  *,
                ROW_NUMBER() OVER (ORDER BY mycolumn) rn
        FROM    mytable
        ) q
WHERE   rn BETWEEN 1001 AND 2000
ORDER BY
        mycolumn

相关文章