Congratulations to all the winners of April 2013, they have won INR 3400 cash and INR 20147 worth prizes !
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 4736 |  Welcome, Guest!   Register  Login
 Home > Code Snippets > SQL Server > Paging in SQL Server ...
Niladri.Biswas

Paging in SQL Server

 Code Snippet posted by: Niladri.Biswas | Posted on: 5/11/2012 | Category: SQL Server Codes | Views: 721 | Status: [Member] | Points: 40 | Alert Moderator   


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

Best Regards,
Niladri Biswas
Found interesting? Add this to:


>> Write Response - Respond to this post and get points

More codes snippets

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/18/2013 6:01:44 PM