Equivalent of LIMIT and OFFSET for SQL Server?

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

    For me the use of OFFSET and FETCH together was slow, so I used a combination of TOP and OFFSET like this (which was faster):

    SELECT TOP 20 * FROM (SELECT columname1, columname2 FROM tablename
        WHERE  ORDER BY columname1 OFFSET 100 ROWS) aliasname
    

    Note: If you use TOP and OFFSET together in the same query like:

    SELECT TOP 20 columname1, columname2 FROM tablename
        WHERE  ORDER BY columname1 OFFSET 100 ROWS
    

    Then you get an error, so for use TOP and OFFSET together you need to separate it with a sub-query.

    And if you need to use SELECT DISTINCT then the query is like:

    SELECT TOP 20 FROM (SELECT DISTINCT columname1, columname2
        WHERE  ORDER BY columname1 OFFSET 100 ROWS) aliasname
    

    Note: The use of SELECT ROW_NUMBER with DISTINCT did not work for me.

提交回复
热议问题