Equivalent of LIMIT and OFFSET for SQL Server?

前端 未结 16 2199
天命终不由人
天命终不由人 2020-11-22 06:07

In PostgreSQL there is the Limit and Offset keywords which will allow very easy pagination of result sets.

What is the equivalent syntax f

16条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 06:47

    select top {LIMIT HERE} * from (
          select *, ROW_NUMBER() over (order by {ORDER FIELD}) as r_n_n 
          from {YOUR TABLES} where {OTHER OPTIONAL FILTERS}
    ) xx where r_n_n >={OFFSET HERE}
    

    A note: This solution will only work in SQL Server 2005 or above, since this was when ROW_NUMBER() was implemented.

提交回复
热议问题