How to use OFFSET and Fetch without Order by in SQL Server

后端 未结 5 1022
我寻月下人不归
我寻月下人不归 2021-02-07 13:35

I want use OFFSET and Fetch in my SQL server 2012 query.But without any order by.I can not use order by.Because my sort order will be lost. How can I use OFFSET and Fetch withou

5条回答
  •  天涯浪人
    2021-02-07 13:55

    You cannot avoid using the required syntax of a ORDER BY with OFFSET and FETCH.

    It is however possible to disassociate the ORDER BY clause that you must provide in order to perform paging from the natural table order created by the record insert process.

    Using the solution below you do not have to make any changes to the underlying table either

    Select 0 as TempSort, T.* From MyTable T ORDER BY TempSort OFFSET 2 ROWS FETCH NEXT 3 ROWS
    

提交回复
热议问题