SQL 服务器 LIMIT 1 和 LIMIT 1,1 语法错误
SQL Server LIMIT 1 and LIMIT 1,1 syntax error
有趣的是,我没有发现 post 这个具体但基本的问题。
目标:更新最新的 budgetid 记录 docstatus = 0。然后我想更新 next-to-last budgetid 记录 docstatus = 1。我在 PHP 但也在我的 SQL 服务器 SEM 中进行测试,它也在那里失败。
我的SQL服务器声明:
select
budgetid, docstatus, datechanged
from
ccy_budget
where
activityid = 11111
order by
datechanged desc
limit 1,1;
SEM 中出现的错误是:
Incorrect syntax near 'limit'.
然而在 w3schools 中这个 [示例] sql 工作得很好:
SELECT *
FROM Customers
ORDER BY postalcode DESC
LIMIT 1,1;
看起来很简单,我肯定遗漏了一些基本的东西。
Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64)
Apr 2 2010 15:48:46
Copyright (c) Microsoft Corporation
Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
SQL 服务器中的等效语法为
select *
from table
order by somerow desc
offset 1 rows fetch next 1 rows only;
但以上内容可从 SQL Server 2012 开始使用,因此对于您的版本,您必须执行以下操作
;with cte
as
(
select *,row_number() over (order by postalcode desc) as rn
from table
)
select * from cte where rn=2
有趣的是,我没有发现 post 这个具体但基本的问题。
目标:更新最新的 budgetid 记录 docstatus = 0。然后我想更新 next-to-last budgetid 记录 docstatus = 1。我在 PHP 但也在我的 SQL 服务器 SEM 中进行测试,它也在那里失败。
我的SQL服务器声明:
select
budgetid, docstatus, datechanged
from
ccy_budget
where
activityid = 11111
order by
datechanged desc
limit 1,1;
SEM 中出现的错误是:
Incorrect syntax near 'limit'.
然而在 w3schools 中这个 [示例] sql 工作得很好:
SELECT *
FROM Customers
ORDER BY postalcode DESC
LIMIT 1,1;
看起来很简单,我肯定遗漏了一些基本的东西。
Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64)
Apr 2 2010 15:48:46
Copyright (c) Microsoft Corporation
Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
SQL 服务器中的等效语法为
select *
from table
order by somerow desc
offset 1 rows fetch next 1 rows only;
但以上内容可从 SQL Server 2012 开始使用,因此对于您的版本,您必须执行以下操作
;with cte
as
(
select *,row_number() over (order by postalcode desc) as rn
from table
)
select * from cte where rn=2