"Invalid usage of the option NEXT in the FETCH statement."

24.5k Views Asked by At

I'm not a SQL master so I'm hoping this isn't too big of an issue. :) Here's my SQL:

query = "SELECT id, email, firstName, lastName " +
        "FROM [WarehouseUser] " +
        "WHERE email LIKE '%' + @Filter + '%' OR FirstName LIKE '%' + @Filter + '%' OR LastName LIKE '%' + @Filter + '%'" +
        "ORDER BY " + "DESC" +
        "OFFSET @Offset ROWS " +
        "FETCH NEXT @Length ROWS ONLY;";

When this is executed I get the following error:

System.Data.SqlClient.SqlException: 'Incorrect syntax near '@Offset'. Invalid usage of the option NEXT in the FETCH statement.'

What am I doing wrong?

1

There are 1 best solutions below

0
On BEST ANSWER

You can see the problem when you output the value of your variable and look at the SQL Query it writes. Here you need a space after "DESC". Like "DESC ". You Currently are having DESCOFFSET instead of DESC OFFSET. See it here:

http://rextester.com/live/RKRXM8567