CREATE PROCEDURE Student_Paging
@SortColumn varchar(50),
@SortDirection varchar(4),
@PageSize int,
@PageNumber int
AS
BEGIN
SELECT
ROW_NUMBER() OVER (ORDER BY (
SELECT CASE @SortColumn
WHEN 'Id' THEN Id
WHEN 'Name' THEN Name
WHEN 'Marks' THEN Marks
END
)
) AS ROWNUMBER,
*
INTO
#Student_Page
FROM
student
IF(@SortDirection = 'ASC')
BEGIN
SELECT *
FROM #Student_Page
WHERE ROWNUMBER BETWEEN ((@PageNumber-1) * @PageSize) + 1 AND (@PageNumber * @PageSize)
ORDER BY ROWNUMBER
END
ELSE IF(@SortDirection = 'DESC')
BEGIN
SELECT *
FROM #Student_Page
WHERE ROWNUMBER BETWEEN ((@PageNumber-1) * @PageSize) + 1 AND (@PageNumber * @PageSize)
ORDER BY ROWNUMBER DESC
END
END
GO
03 September, 2010
Paging And Sorting In Sql Server OR Getting Middle Rows In Sql Server
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment