Equivalent of LIMIT and OFFSET for SQL Server?

前端 未结 16 1933
天命终不由人
天命终不由人 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:48

    Adding a slight variation on Aaronaught's solution, I typically parametrize page number (@PageNum) and page size (@PageSize). This way each page click event just sends in the requested page number along with a configurable page size:

    begin
        with My_CTE  as
        (
             SELECT col1,
                  ROW_NUMBER() OVER(ORDER BY col1) AS row_number
         FROM
              My_Table
         WHERE
              <<>>
        )
        select * from My_CTE
                WHERE RowNum BETWEEN (@PageNum - 1) * (@PageSize + 1) 
                                  AND @PageNum * @PageSize
    
    end
    

提交回复
热议问题