SQL Server paging query

后端 未结 9 1313
攒了一身酷
攒了一身酷 2021-01-06 16:48

Urggggg! I\'ve been struggling with this for a long time! I can do it with MySQL so easy but not with SQL Server :(

Here are the simplified tables which should be jo

9条回答
  •  孤街浪徒
    2021-01-06 17:39

    Microsoft added native paging features in SQL Server 2012 and above using "OFFSET" and "FETCH". You can use this feature as below:

    -- Skip the first 500 rows and return the next 100
    SELECT *
    FROM TableName
    ORDER BY [ID]
        OFFSET 500 ROWS
        FETCH NEXT 100 ROWS ONLY;
    

    For the OFFSET __ and FETCH NEXT __ clauses, you can specify constant values (as above), or you can specify variables, expressions, or constant scalar subqueries.

提交回复
热议问题