SET @StartRow = (@PageIndex - 1) * @PageSize + 1
SET @EndRow = @StartRow + @PageSize - 1
SET @StartRow = (@PageIndex - 1) * @PageSize + 1
SET @EndRow = @StartRow + @PageSize - 1
SET @TotalRec = Select Count(*) from Test
-- If StartRow is less then Total Records then fetch all records between startrow and endrow else ignore start row only consider endrow
IF @StartRow <= @TotalRecords
SET @SQL = '
SELECT *
FROM (
SELECT *,
ROW_NUMBER() OVER (ORDER BY '+@Sort+') as [RowNumber]
from Test
)T1
WHERE RowNumber >= @StartRow AND RowNumber <= @EndRow'
SET @ParmDefinition = N'@StartRow int,@EndRow int,@Sort nvarchar(50)';
EXECUTE sp_executesql @SQL,@ParmDefinition,@StartRow=@StartRow,@EndRow=@EndRow,@Sort=@Sort;
END
ELSE
BEGIN
SET @SQL = '
SELECT *
FROM (
SELECT *,
ROW_NUMBER() OVER (ORDER BY '+@Sort+') as [RowNumber]
from Test
)T1
WHERE RowNumber <= @EndRow'
SET @ParmDefinition = N'@StartRow int,@EndRow int,@Sort nvarchar(50)';
EXECUTE sp_executesql @SQL,@ParmDefinition,@StartRow=@StartRow,@EndRow=@EndRow,@Sort=@Sort;
END
RETURN @TotalRec
No comments:
Post a Comment