Saturday, May 31, 2014

SQL SERVER SELECT USING PAGING DATA

begin
DECLARE @intPage INT =4
DECLARE  @intPageSize INT =20

DECLARE @intStartRow int;
DECLARE @intEndRow int;
SET @intStartRow = (@intPage -1) * @intPageSize + 1;
SET @intEndRow = @intPage * @intPageSize;  
WITH blogs AS
    (SELECT USER_FULL_NAME,
     ROW_NUMBER() OVER(ORDER BY USER_ID DESC) as intRow,
     COUNT(USER_ID) OVER() AS intTotalHits
     FROM USERS)
SELECT USER_FULL_NAME, intTotalHits FROM blogs
WHERE intRow BETWEEN @intStartRow AND @intEndRow
end

No comments:

Post a Comment