SQL 服务器 'FETCH FIRST 1 ROWS ONLY' 无效用法
SQL Server 'FETCH FIRST 1 ROWS ONLY' Invalid usage
我正在尝试将 Db2 查询转换为 SQL 服务器,我遇到了一个我不熟悉的结构:仅获取前 1 行。
这是在 db2 上运行的查询:
select * from products.series where state = 'xxx' order by id
FETCH FIRST 1 ROWS ONLY
以及我在 SQL 服务器上遇到的错误:
Invalid usage of the option FIRST in the FETCH statement.
我尝试用 SQL 服务器似乎允许的 NEXT 替换 FIRST,但没有成功。
我正在使用 SQL 服务器 2014
使用top
:
select top 1 * from products.series where state = 'xxx' order by id
尝试使用 OFFSET
子句
select * from products.series where state = 'xxx' order by id
OFFSET 0 ROWS
FETCH NEXT 1 ROWS ONLY
您可以使用 top() 函数'
select top 1 * from table
SELECT TOP 1 * FROM (select * 来自 products.series where state = 'xxx') as tmp ORDER BY id
我正在尝试将 Db2 查询转换为 SQL 服务器,我遇到了一个我不熟悉的结构:仅获取前 1 行。
这是在 db2 上运行的查询:
select * from products.series where state = 'xxx' order by id
FETCH FIRST 1 ROWS ONLY
以及我在 SQL 服务器上遇到的错误:
Invalid usage of the option FIRST in the FETCH statement.
我尝试用 SQL 服务器似乎允许的 NEXT 替换 FIRST,但没有成功。
我正在使用 SQL 服务器 2014
使用top
:
select top 1 * from products.series where state = 'xxx' order by id
尝试使用 OFFSET
子句
select * from products.series where state = 'xxx' order by id
OFFSET 0 ROWS
FETCH NEXT 1 ROWS ONLY
您可以使用 top() 函数'
select top 1 * from table
SELECT TOP 1 * FROM (select * 来自 products.series where state = 'xxx') as tmp ORDER BY id