1

I have a table with 100 rows, and want to SELECT just a part of them(for example from row 20 to 30), like "paging" the SELECT.

which is an efficient way to do that in SQL Server 2008 R2?

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
Logar314159
  • 503
  • 4
  • 16

1 Answers1

3

you can do it like this (assuming your table is called "tablename" ,your primary key is id, and you want rows 10 to 15).

select * from 
(select *,row_number() over (order by id) as r from tablename)
t where r >10 and r < 15;

it may look inefficient but this is (sort of) how its done internally in linq.

shaish
  • 1,479
  • 1
  • 12
  • 23
  • seems to be the same algorithm than [How to return a page of results from SQL?](http://stackoverflow.com/questions/10190/how-to-return-a-page-of-results-from-sql) thank you. – Logar314159 Mar 27 '13 at 22:24