C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » SQL »

Store Procedure to be Used in Custom Pagging


Posted Date: 26 Sep 2009    Resource Type: Code Snippets    Category: SQL
Author: Lakhan PalMember Level: Diamond    
Rating: 1 out of 5Points: 10



Custom paging



With the help of the below set of query we can get the number of record that we want to show to user.

For this we need to pass two parameters. first is the @RecordsToPick (Number of Records that you want to picjk for the page) and the second is @PageNumber (Page number for which you want to get the records from Database.)

USE DBName
GO
DECLARE @RecordsToPick smallint, @PageNumber smallint
SET @RecordsToPick = 10
SET @PageNumber = 2

DECLARE @StartRow INT
DECLARE @EndRow INT
SET @StartRow = ((@PageNumber-1) * @RecordsToPick)
SET @EndRow = @StartRow + @RecordsToPick

SELECT * FROM ( SELECT UserName,UserID,City,State,Country ROW_NUMBER()OVER(ORDER BY UserID) AS RowNumber
FROM as_TblMembers) As AliasName WHERE RowNumber > @StartRow AND RowNumber <= @EndRow
GO


In the above store procedure first we will get the @StartRow and @EndRow to get the number of first record and last record respectively.

ROW_NUMBER()OVER(ORDER BY UserID) AS RowNumber
will assign a unique number to each query order by UserID. with the help of it is easy for us to get the required result.

Thanks & Regards
Lakhan Pal Garg
Free Code Snippets
http://lakhangarg.blogspot.com



Responses

Author: Alwyn    11 Nov 2009Member Level: Gold   Points : 2
This will not work accurately it may go wrong 90%.

Suppose, i have 4 records and doing paging by 2 rows per page

1 - q
2 - w
3 - e
4 - r

when i run the query for the first time, it will show

1 - q
2 - w

in the mean while, some user has removed the 2nd record
so the remaining values will be like;

1 - q
3 - e
4 - r

now when we run the query for the second time, it will display only

4 - r

but it should show

3 - e
4 - r

henc we cannot use this code.



Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
SQL Server  .  Custom Paging  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Sql Replace Function
Previous Resource: Change Database Name-Sql
Return to Discussion Resource Index
Post New Resource
Category: SQL


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use