10

我正在尝试将 Db2 查询转换为 SQL Server,但遇到了一个我不熟悉的构造:仅 FETCH FIRST 1 ROWS。

这是在 db2 上运行的查询:

select * from products.series where state = 'xxx' order by id 
FETCH FIRST 1 ROWS ONLY

以及我在 SQL Server 上遇到的错误:

Invalid usage of the option FIRST in the FETCH statement.

我尝试用 SQL Server 中似乎承认的 NEXT 替换 FIRST,但没有成功。

我正在使用 SQL Sever 2014

4

4 回答 4

17

尝试使用OFFSET子句

select * from products.series where state = 'xxx' order by id 
OFFSET 0 ROWS
FETCH NEXT 1 ROWS ONLY
于 2017-04-26T15:56:15.770 回答
14

使用top

select top 1 * from products.series where state = 'xxx' order by id 
于 2017-04-26T15:47:55.750 回答
2

您可以使用 top() 函数'

select top 1 * from table
于 2017-04-26T16:30:51.127 回答
0

SELECT TOP 1 * FROM (select * from products.series where state = 'xxx') as tmp ORDER BY id

于 2022-02-13T01:48:48.493 回答