Paging in SQL Server

Niladri.Biswas
Posted by Niladri.Biswas under Sql Server category on | Points: 40 | Views : 1743
Declare @StartFrom INT = 1
Declare @MaximumRows INT = 25

;With Cte(Id,Name,PhoneNo) As (
Select
Id = 1
,Name='Name' + CAST( 1 As Varchar(50))
, PhoneNo=12345678
Union All
Select
Id+1
,Name= 'Name' + CAST( Id+1 As Varchar(50))
, PhoneNo+1
From Cte
Where Id <100
)

SELECT *
FROM Cte
WHERE ID
BETWEEN ((@StartFrom -1)* @MaximumRows) + 1
AND @StartFrom * @MaximumRows




Result

Id	Name	PhoneNo
1 Name1 12345678
2 Name2 12345679
3 Name3 12345680
4 Name4 12345681
5 Name5 12345682
6 Name6 12345683
7 Name7 12345684
8 Name8 12345685
9 Name9 12345686
10 Name10 12345687
11 Name11 12345688
12 Name12 12345689
13 Name13 12345690
14 Name14 12345691
15 Name15 12345692
16 Name16 12345693
17 Name17 12345694
18 Name18 12345695
19 Name19 12345696
20 Name20 12345697
21 Name21 12345698
22 Name22 12345699
23 Name23 12345700
24 Name24 12345701
25 Name25 12345702



Hope this may help

Comments or Responses

Login to post response