How to use limit in Microsoft SQL Server 2008?

2019-08-17 13:58发布

We have to use Limit keyword in SQL server 2008/2012.

We need to apply limit for every query where start index will change every time. When I was googling found TOP but it won't work for us. Can anyone please share how to use LIMIT keyword in sql server where every time start index change.

We need query in SQL server like below -

SELECT * from STOCK LIMIT 11, 10000 (where 11=start index, 10000=size)

4条回答
相关推荐>>
2楼-- · 2019-08-17 14:02

You could use a CTE with a window function, like in the answer to this SO question -> Skip first row in SQL Server 2005?

查看更多
forever°为你锁心
3楼-- · 2019-08-17 14:05

We have upgraded to SQL server 2012 and replaced query with OFFSET and FETCH. Sample is below.

SELECT ITEM_ID, PRICE FROM MENU ORDER BY ITEM_ID ASC OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY; 
查看更多
来,给爷笑一个
4楼-- · 2019-08-17 14:16

You would have to use something like the ROW_NUMBER() function and then specify what you want from there.

select top 10000 *, row_number() over (order by [YourFieldName]) as row from Stock where row > 11
查看更多
Juvenile、少年°
5楼-- · 2019-08-17 14:24

This is probably not a good long-term solution for you, but if the table has an Identity field you could do something like this:

SELECT TOP 1000 * FROM STOCK
WHERE Id > 11
ORDER BY Id
查看更多
登录 后发表回答