I have a project based on SQL Server 2008. There is table with millions of rows, I need to fetch some rows between two dates (there is a column cdate
), need to output a sheet file for the same, but it's not possible to fetch all rows at once(request timeout problem), so I find a way to break in multiple requests to write on sheet. But for this I need to have a way to select only some rows like limit and offset in mysql. Is there a way to do this???
Asked
Active
Viewed 1,583 times
-2

marc_s
- 732,580
- 175
- 1,330
- 1,459

Eshant Sahu
- 360
- 1
- 4
- 18
1 Answers
0
Microsoft Transact-SQL equivalent of limit
is top
.
select top 100
whicheverColumns
from
whicheverTable
where
whicheverFilteringCriteria
order by
whicheverDateEtc desc; -- or ASC is default

underscore_d
- 6,309
- 3
- 38
- 64
-
But I need to select in between rows too, how can it be done by select top clause?? – Eshant Sahu Jul 19 '15 at 19:38
-
The above duplicate link has an answer indicating how you can do this by adding a subquery/CTE containing `ROW_NUMBER() `. – underscore_d Jul 19 '15 at 19:41
-
@underscore_d Or you can use an API cursor to pick up where you left off the previous batch without having to recalculate it. http://dba.stackexchange.com/a/68280/3690 – Martin Smith Jul 19 '15 at 19:47
-
@MartinSmith interesting idea, thanks for the link! That would definitely be preferable for large/repeated blocks to avoid rescans. – underscore_d Jul 19 '15 at 19:51