Hi All,
I want to fetch records from a table in my sql server database in parts. Like, in one query I want to see first 1000 records, in next query next 1000 records. Likewise..
Is it possible with sql server ? I am using sql server 2008. While googling, I found LIMIT clause for mysql, but it does not work for sql server. Please help.
Loading

Dorababu MekaRank #457 · 5.5K reputationPosted May 4, 2011, 5:20 AM EDT
First 1000 records:
SELECT TOP 1000 * FROM mytable ORDER BY mycolumnGeneral solution (supports offset)
SELECT * FROM ( SELECT *, ROW_NUMBER() OVER (ORDER BY mycolumn) rn FROM mytable ) q WHERE rn BETWEEN 1001 AND 2000 ORDER BY mycolumnVijay YadavRank #1,637 · 943 reputationPosted May 4, 2011, 5:05 AM EDT
You are right LIMIT clause doesn't work for Sql as it work with MySql as far as i am concerned.
I think it can be done by using loop, while fecthing the records from the database you can apply your logic if the record reach to the 1000th record you can break the loop and then you can fetch the next 1000 record.
Dorababu MekaRank #457 · 5.5K reputationPosted May 4, 2011, 5:08 AM EDT
select top 1000 * from tblname where condition
select top 2000 * from tblname where condition minus select top 1000 * from tblname where condition
Suhani Mody23 reputationPosted May 4, 2011, 5:16 AM EDT
what about next 1000 records ? ;)
Suhani Mody23 reputationPosted May 4, 2011, 5:17 AM EDT
Suhani Mody23 reputationPosted May 4, 2011, 5:31 AM EDT
Dorababu MekaRank #457 · 5.5K reputationPosted May 4, 2011, 5:34 AM EDT
Mark as answer
Vijay YadavRank #1,637 · 943 reputationPosted May 4, 2011, 5:35 AM EDT
Sam HobbsRank #107 · 20.5K reputationPosted May 4, 2011, 1:23 PM EDT