Simo
Simo

Reputation: 67

SQL Server TOP clause

I have some difficult with this clause.

How can I select rows with an interval? I mean like clause limit in MySQL

For example we have this table called "TB":

 id | brand
----|-----
  1 | samsung
  2 | hp
  3 | microsoft
  4 | apple
  5 | sony
  6 | acer
  7 | google
  8 | facebook

I want to select the rows between 3 and 6. In MySQL there is a clause limit wish do that:

select * from TB limit 2,3

The question is how can I do it with clause TOP?

I want to use this clause in a page asp.net wish it display all products by receiving a parameter of page.

How can I do it with SQL Server top clause ?

Upvotes: 0

Views: 100

Answers (1)

AK47
AK47

Reputation: 3807

As you are using SQL Server 2012, I think you require "Offset & Fetch Next" Please try following

Declare @startFrom int = 5 ---- From which row you want to start
Declare @recCount int = 2  ----- No of records you want to Fetch

select * from myTable
order by id
offset @startFrom rows
fetch next @recCount rows only

SQL Fiddle DEMO

Upvotes: 1

Related Questions