SQL Server 'FETCH FIRST 1 ROWS ONLY' 无效用法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43638944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 05:13:07  来源:igfitidea点击:

SQL Server 'FETCH FIRST 1 ROWS ONLY' Invalid usage

sqlsql-serverdb2

提问by Argentina

I am trying to convert a Db2 query to SQL Server, I came across a construct I am not familiar with: FETCH FIRST 1 ROWS ONLY.

我正在尝试将 Db2 查询转换为 SQL Server,但遇到了一个我不熟悉的构造:仅获取第一行。

This is the query working on db2:

这是在 db2 上运行的查询:

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

and the error I am getting on SQL Server:

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

Invalid usage of the option FIRST in the FETCH statement.

I have tried replacing FIRST with NEXT which seems to be admitted in SQL Server, but with no success.

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

I am using SQL Sever 2014

我正在使用 SQL Server 2014

回答by Oto Shavadze

Try with OFFSETclause

尝试OFFSET从句

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

回答by SqlZim

use top:

使用top

select top 1 * from products.series where state = 'xxx' order by id 

回答by MileP

You can use top() finction'

您可以使用 top() 功能'

select top 1 * from table