Skip to content

Instantly share code, notes, and snippets.

@cemerson
Created February 7, 2014 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cemerson/8867473 to your computer and use it in GitHub Desktop.
Save cemerson/8867473 to your computer and use it in GitHub Desktop.
SQL: Paging in Stored Procedure
DECLARE @intStartRow int;
DECLARE @intEndRow int;
SET @intStartRow = (@intPage -1) * @intPageSize + 1;
SET @intEndRow = @intPage * @intPageSize;
WITH blogs AS
(SELECT strBlogName,
ROW_NUMBER() OVER(ORDER BY intID DESC) as intRow,
COUNT(intID) OVER() AS intTotalHits
FROM tblBlog)
SELECT strBlogName, intTotalHits FROM blogs
WHERE intRow BETWEEN @intStartRow AND @intEndRow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment