3

in MySQL we use LIMIT and in sql server we must use TOP instead of LIMIT,but we can use LIMIT in the this way:

Limit X, Y which X is the starting point and Y is duration.

now how can i use TOP like that example?

Reza M.A
  • 1,197
  • 1
  • 16
  • 33

2 Answers2

4

you can use ROW_NUMBER() function , with CTE (Common table Expression)

ROW_NUMBER

with cte as(
select *,ROW_NUMBER() over (order by col1) as rownumber from <table>)
select * from cte where rownumber between x and y 
Joe G Joseph
  • 23,518
  • 5
  • 56
  • 58
3

You can't before SQL Server 2012 which added FETCH..OFFSET

So you need to "page" using ROW_NUMBER function. Examples:

Community
  • 1
  • 1
gbn
  • 422,506
  • 82
  • 585
  • 676
  • Even in SQL Server 2000 wouldn't this work? `SELECT TOP 5 * FROM (SELECT TOP 10 * FROM tbl ORDER BY id DESC) AS data ORDER BY id ASC` Perhaps not recommended in the case of other options, but at least it exists... – MatBailie Aug 08 '12 at 12:51
  • 1
    @Dems: yes. I assumed that folk would be on SS 2005 at least. In 2012 ;-) – gbn Aug 08 '12 at 12:57