Equivalent of LIMIT and OFFSET for SQL Server?

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

    Since, I test more times this script more useful by 1 million records each page 100 records with pagination work faster my PC execute this script 0 sec while compare with mysql have own limit and offset about 4.5 sec to get the result.

    Someone may miss understanding Row_Number() always sort by specific field. In case we need to define only row in sequence should use:

    ROW_NUMBER() OVER (ORDER BY (SELECT NULL))

    SELECT TOP {LIMIT} * FROM (
          SELECT TOP {LIMIT} + {OFFSET} ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS ROW_NO,*
          FROM  {TABLE_NAME}
    ) XX WHERE ROW_NO > {OFFSET}
    

    Explain:

    • {LIMIT}: Number of records for each page
    • {OFFSET}: Number of skip records

提交回复
热议问题