Search code examples
sqlsql-serverdb2sql-fetch

SQL Server 'FETCH FIRST 1 ROWS ONLY' Invalid usage


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.

This is the query working on 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:

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.

I am using SQL Sever 2014


Solution

  • Try with OFFSET clause

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