0

I want to take le's say ten records but skip the first 20? I know how to do that in linq, but how to do it in TSqL.

In linq would be something like this:

qp = dbContext.Products.Where(p => p.Active).OrderBy(p => p.Name)
                .Skip((curPage - 1) * prodPerPage).Take(prodPerPage);
user1238784
  • 2,250
  • 3
  • 22
  • 41

1 Answers1

0

In SQL Server (and most other databases), ROW_NUMBER offers a general way to do this:

WITH cte AS (
    SELECT *, ROW_NUMBER() OVER (ORDER BY some_column) rn
    FROM yourTable
)

SELECT *
FROM cte
WHERE rn BETWEEN 21 AND 30; -- skip the first 20 records, and keep the 10 after that
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360