1

我有这个剃刀声明

sql = "SELECT * FROM CarBike" +
       "Order By id OFFSET @0 ROWS FETCH NEXT @1 ROWS ;";

var result = db.Query(sql, offset, pageSize);

我收到错误

Incorrect syntax near the keyword 'By'.
Invalid usage of the option NEXT in the FETCH statement.

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

请帮我纠正这个错误

4

1 回答 1

3

CarBike和之间需要一个空格Order by

sql = "SELECT * FROM CarBike" + 
   " Order By id OFFSET @0 ROWS FETCH NEXT @1 ROWS ;"; 

注意:OFFSET/FETCH仅限 SQL 2012+。

在以前的版本中达到类似的结果

select * from
(
select *, ROW_NUMBER() over (order by id) rn
from CarBike
) v
where rn between @0+1 and @0+@1
order by id
于 2012-09-13T09:02:56.943 回答