SQL Paging Stored procedure to filter data.
CREATE PROCEDURE GetWorlds --@start=1,@end=5,@sortingType='desc', @SortingColumn='1', @userId = 1
@start BIGINT = 0
,@end BIGINT = 10
,@sortingType VARCHAR(20) = 'desc'
,@SortingColumn varchar(10)='1'
,@userId int
AS
BEGIN
Declare @SortCol as varchar(10) = @SortingColumn + @sortingType;
SELECT *,(
SELECT COUNT(*) FROM World WHERE CreatedBy = @userId) AS TotalRecords
FROM (SELECT ROW_NUMBER() OVER (
ORDER BY (case @SortCol when '1asc' then ID end) asc,
(case @SortCol when '1desc' then ID end) desc,
(case @SortCol when '2asc' then WorldName end) asc,
(case @SortCol when '2desc' then WorldName end) desc --you can specify more column here to sorting.
) AS ROW,*
FROM World
WHERE (CreatedBy = @userId) --all the filter will check here.
) AS DATA
WHERE (ROW > @start AND ROW <= @end + @start)
END
Note: In this stored procedure value of @start parameter should be less then the start number of record. Suppose you want to get records from 1 to 10. Then the value of @start parameter would be 0.
No comments:
Post a Comment