Making sense of 'OFFSET/FETCH' in SSMS 2012

点点圈 提交于 2019-12-23 02:05:13

问题


Just installed Microsoft SQL Server Management Studio 2012 today. In familiarizing myself with the pagination feature addition of ORDER BY, I keep running into this error:

Msg 102, Level 15, State 1, Line 5
Incorrect syntax near 'OFFSET'.
Msg 153, Level 15, State 2, Line 6
Invalid usage of the option NEXT in the FETCH statement.

Here is my query:

    SELECT SingleWomansName, NumberOfCats
    FROM CatLadies
    WHERE NumberOfCats > 1
    ORDER BY NumberOfCats
    OFFSET 10 ROWS
    FETCH NEXT 5 ROWS ONLY

I've seen plenty of how-to articles with similar syntax. What gives?

http://msdn.microsoft.com/en-us/library/gg699618.aspx


回答1:


Are you positive you are connected to a SQL Server 2012 database? The following works:

Select Name, CatCount
From CatLadies
Order By Name
Offset 2 Rows 
Fetch Next 2 Rows Only

SQL Fiddle version

I do get the error you are seeing when I switch to SQL Server 2008

SQL Fiddle using SQL Server 2008




回答2:


Make sure your database compatibility level is set to SQL Server 2012 (110).



来源:https://stackoverflow.com/questions/11270373/making-sense-of-offset-fetch-in-ssms-2012

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!