Click here to Skip to main content
15,888,031 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi guys i used limit to fetch rows with range ? but my code query do have an error heres my code:

What I have tried:

cmd.CommandText = "select * FROM tblstudinfo LIMIT 1,2"
rdr = cmd.ExecuteReader


it said "incorrect syntax near 1"
Posted
Updated 24-Jan-17 5:23am

If you're using MS SQL Server 2012 or later, you can use OFFSET and FETCH:
SQL
SELECT *
FROM tblstudinfo
ORDER BY studentid
OFFSET 1 ROWS
FETCH NEXT 2 ROWS ONLY

ORDER BY Clause (Transact-SQL)[^]
 
Share this answer
 
In MS Sql you use the TOP keyword.

SQL
SELECT TOP 1 * FROM tblStudInfo
 
Share this answer
 
Comments
Member 12919944 24-Jan-17 8:09am    
but it will only select from the top most ? i want to select from different range of rows. for example rows 5 to 10 only or 1 to 5
ZurdoDev 24-Jan-17 8:15am    
In that case, you use ROW_NUMBER https://msdn.microsoft.com/en-us/library/ms186734.aspx.
Member 12919944 24-Jan-17 8:19am    
i tried it but it said invalid column name ROW_NUMBER
Member 12919944 24-Jan-17 8:19am    
cmd.CommandText = "select * FROM tblstudinfo where ROW_NUMBER BETWEEN 1 and 2"
ZurdoDev 24-Jan-17 8:21am    
You need to read the documentation. That is not the syntax for ROW_NUMBER.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900