为什么这不起作用,并给我“偏移量命令附近的语法不正确”错误
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
检查数据库兼容性级别。 OFFSET是在SQL Server 2012中添加的,因此如果您的数据库处于2008兼容模式,则此关键字不可用。
OFFSET
查看或更改数据库的兼容性级别
在T-SQL中,您可以像这样检查它:
USE AdventureWorks2012; GO SELECT compatibility_level FROM sys.databases WHERE name = 'AdventureWorks2012'; GO
这是兼容性级别的列表:
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
此外,Azure SQL数据仓库和并行数据仓库不支持该OFFSET子句,从该ORDER BY子句的文档可以看出:
ORDER BY
-- 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 ] } -- Syntax for Azure SQL Data Warehouse and Parallel Data Warehouse [ ORDER BY { order_by_expression [ ASC | DESC ] } [ ,...n ] ]
-- 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 ] }
-- Syntax for Azure SQL Data Warehouse and Parallel Data Warehouse [ ORDER BY { order_by_expression [ ASC | DESC ] } [ ,...n ] ]