为什么这不起作用,并且在偏移命令附近给出了一个"不正确的语法"错误
SELECT o.orderdate, o.orderid, o.empid, o.custid FROM Sales.Orders o
ORDER BY o.orderdate, o.orderid
OFFSET 50 ROWS
FETCH NEXT 25 ROWS ONLY;
我正在使用SQL Server Express 2014
答案 0 :(得分:11)
检查数据库兼容级别。
SQL Server 2012中添加了 USE AdventureWorks2012;
GO
SELECT compatibility_level
FROM sys.databases WHERE name = 'AdventureWorks2012';
GO
,因此如果您的数据库处于2008兼容模式,则此关键字不可用。
View or Change the Compatibility Level of a Database
在T-SQL中,您可以这样检查:
65 - SQL Server 6.5
70 - SQL Server 7.0
80 - SQL Server 2000
90 - SQL Server 2005
100 - SQL Server 2008/R2
110 - SQL Server 2012
120 - SQL Server 2014
130 - SQL Server 2016
140 - SQL Server 2017
以下是从How to check SQL Server Database compatibility after sp_dbcmptlevel is deprecated?获取的兼容性级别列表:
OFFSET
此外,Azure SQL数据仓库和并行数据仓库不支持-- Syntax for SQL Server and Azure SQL Database
ORDER BY order_by_expression
[ COLLATE collation_name ]
[ ASC | DESC ]
[ ,...n ]
[ <offset_fetch> ]
<offset_fetch> ::=
{
OFFSET { integer_constant | offset_row_count_expression } { ROW | ROWS }
[
FETCH { FIRST | NEXT } {integer_constant | fetch_row_count_expression } { ROW | ROWS } ONLY
]
}
子句,可以从ORDER BY
子句的文档中看出:
-- Syntax for Azure SQL Data Warehouse and Parallel Data Warehouse [ ORDER BY { order_by_expression [ ASC | DESC ] } [ ,...n ] ]
{{1}}
答案 1 :(得分:1)
我的问题是我试图在视图上使用OFFSET
,但没有加入。
问题查询:
declare @PageSize int = 25;
declare @PageNumber int = 1;
with countCte as
(
select count(*) as TotalCount from vw_viewName vn
where 1=1
)
select * from vw_viewName vn
cross join countCte cou
where 1=1
OFFSET @PageSize * (@PageNumber - 1) ROWS
FETCH NEXT @PageSize ROWS ONLY OPTION (RECOMPILE)
添加order by
可以解决问题:
where 1=1
order by vn.ID desc
OFFSET @PageSize * (@PageNumber - 1) ROWS
FETCH NEXT @PageSize ROWS ONLY OPTION (RECOMPILE)