You need to do something like this:
;WITH PaginatingData AS
(
Select id, name, ROW_NUMBER() OVER (ORDER BY id asc) as 'RowNo'
from customers
)
SELECT *
FROM PaginatingData
where RowNo between 50 AND 60
Use a CTE (Common Table Expression - sort of an "inline view") as a "wrapper" so that your RowNo
becomes a valid column name.
As an outlook - with SQL Server 2012, you'd be able to write something like this:
SELECT
id, name
FROM
dbo.customers
ORDER BY
id
OFFSET 50 ROWS
FETCH NEXT 10 ROWS ONLY
SQL Server 2012 will have this ANSI SQL Standard compliant notation to do paging directly based on an ORDER BY
clause. See this blog post (or tons of others) for more info and more samples.
'string delimiters'
for aliases. This syntax is non-standard, can be very confusing (e.g.SELECT 'a' 'b'
), and is deprecated in SQL Server. Use[square brackets]
(also not standard), or"double quotes"
(standard), or no delimiters. For more info and lots of debate see sqlblog.org/blogs/aaron_bertrand/archive/2012/01/23/…