<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>SQL Server 2005 - Data Paging</title><link>http://weblogs.asp.net/firoz/archive/2005/06/12/411949.aspx</link><description>It now becomes a UI standard for displaying records in paginated grid format. If you have list of employees then you always wanted to only show 10 (or something) at a time. And if user wants to see next set of employees he should click on the next button</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>re: SQL Server 2005 - Data Paging</title><link>http://weblogs.asp.net/firoz/archive/2005/06/12/411949.aspx#7107317</link><pubDate>Wed, 03 Jun 2009 18:46:38 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7107317</guid><dc:creator>firoz.ansari</dc:creator><author>firoz.ansari</author><description>&lt;p&gt;I am not sure but you don't need four separate queries for First/Prev/Next/Last link. Only one SQL statement will fetch data based on which &amp;quot;page&amp;quot; you want to show. Modified SQL will be look like this:&lt;/p&gt;
&lt;p&gt;WITH ClientList AS (&lt;/p&gt;
&lt;p&gt;SELECT ROW_NUMBER() OVER (ORDER BY CompanyID) AS RowNumber, &amp;lt;field list&amp;gt;&lt;/p&gt;
&lt;p&gt;FROM Clients&lt;/p&gt;
&lt;p&gt;)&lt;/p&gt;
&lt;p&gt;SELECT *&lt;/p&gt;
&lt;p&gt;FROM ClientList&lt;/p&gt;
&lt;p&gt;WHERE RowNumber between 1 and 10&lt;/p&gt;
&lt;p&gt;Put above SQL in SP and pass two additional arguments, PageIndex &amp;amp; PageSize which used to determine lower and upper record number.&lt;/p&gt;
&lt;p&gt;In SP:&lt;/p&gt;
&lt;p&gt;SET @PageLowerBound = @PageSize * @PageIndex&lt;/p&gt;
&lt;p&gt;SET @PageUpperBound = @PageLowerBound + @PageSize&lt;/p&gt;
&lt;p&gt;WITH ClientList AS (&lt;/p&gt;
&lt;p&gt;SELECT ROW_NUMBER() OVER (ORDER BY CompanyID) AS RowNumber, &amp;lt;field list&amp;gt;&lt;/p&gt;
&lt;p&gt;FROM Clients&lt;/p&gt;
&lt;p&gt;)&lt;/p&gt;
&lt;p&gt;SELECT *&lt;/p&gt;
&lt;p&gt;FROM ClientList&lt;/p&gt;
&lt;p&gt;WHERE RowNumber between @PageLowerBound and @PageUpperBound&lt;/p&gt;
&lt;p&gt;Page will hold the current page number. If you click on Next, add +1 to the current page and pass it to this SP. Same way, if you click on Prev, substract 1 from current page.&lt;/p&gt;
&lt;p&gt;Hope this help.&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7107317" width="1" height="1"&gt;</description></item><item><title>re: SQL Server 2005 - Data Paging</title><link>http://weblogs.asp.net/firoz/archive/2005/06/12/411949.aspx#7107266</link><pubDate>Wed, 03 Jun 2009 17:52:21 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7107266</guid><dc:creator>Yahya</dc:creator><author>Yahya</author><description>&lt;p&gt;Faraz, this is a related but is from access. I have four sql that bring first, next, prev and last recs respectively as below, not sure how to implement in sql server using ROW_NUMBER(), if at all this is feasible. Wonder if you can give some pointers.&lt;/p&gt;
&lt;p&gt;First&lt;/p&gt;
&lt;p&gt;SELECT TOP 1 &amp;lt;field list&amp;gt;&lt;/p&gt;
&lt;p&gt;FROM Clients&lt;/p&gt;
&lt;p&gt;WHERE (Status = @Status)&lt;/p&gt;
&lt;p&gt;ORDER BY CompanyID&lt;/p&gt;
&lt;p&gt;Next&lt;/p&gt;
&lt;p&gt;SELECT TOP 1 &amp;lt;field list&amp;gt;&lt;/p&gt;
&lt;p&gt;FROM Clients&lt;/p&gt;
&lt;p&gt;WHERE (CompanyID &amp;gt; ?) AND (Status = @Status)&lt;/p&gt;
&lt;p&gt;ORDER BY CompanyID&lt;/p&gt;
&lt;p&gt;Prev&lt;/p&gt;
&lt;p&gt;SELECT TOP 1 &amp;lt;field list&amp;gt;&lt;/p&gt;
&lt;p&gt;FROM Clients&lt;/p&gt;
&lt;p&gt;WHERE (CompanyID &amp;lt; ?) AND (Status = @Status)&lt;/p&gt;
&lt;p&gt;ORDER BY CompanyID DESC&lt;/p&gt;
&lt;p&gt;Last&lt;/p&gt;
&lt;p&gt;SELECT TOP 1 &amp;lt;field list&amp;gt;&lt;/p&gt;
&lt;p&gt;FROM Clients&lt;/p&gt;
&lt;p&gt;WHERE (Status = @Status)&lt;/p&gt;
&lt;p&gt;ORDER BY CompanyID DESC&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7107266" width="1" height="1"&gt;</description></item><item><title>re: SQL Server 2005 - Data Paging</title><link>http://weblogs.asp.net/firoz/archive/2005/06/12/411949.aspx#6741560</link><pubDate>Sun, 16 Nov 2008 18:22:09 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6741560</guid><dc:creator>Estetik</dc:creator><author>Estetik</author><description>&lt;p&gt;I&amp;#39;m guessing the only way to do this in Javascript would be to use the onresize event, and then using the resizeTo method to attempt to keep the window at the size you want? &lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6741560" width="1" height="1"&gt;</description></item><item><title>re: SQL Server 2005 - Data Paging</title><link>http://weblogs.asp.net/firoz/archive/2005/06/12/411949.aspx#6684456</link><pubDate>Fri, 17 Oct 2008 12:37:31 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6684456</guid><dc:creator>DragonGod</dc:creator><author>DragonGod</author><description>&lt;p&gt;This is excellent.&lt;/p&gt;
&lt;p&gt;Thank you.&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6684456" width="1" height="1"&gt;</description></item><item><title>re: SQL Server 2005 - Data Paging</title><link>http://weblogs.asp.net/firoz/archive/2005/06/12/411949.aspx#6119713</link><pubDate>Mon, 21 Apr 2008 21:26:25 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6119713</guid><dc:creator>Okey</dc:creator><author>Okey</author><description>&lt;p&gt;very thanks&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6119713" width="1" height="1"&gt;</description></item><item><title>re: SQL Server 2005 - Data Paging</title><link>http://weblogs.asp.net/firoz/archive/2005/06/12/411949.aspx#5786172</link><pubDate>Thu, 14 Feb 2008 15:53:27 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5786172</guid><dc:creator>firoz.ansari</dc:creator><author>firoz.ansari</author><description>&lt;p&gt;AT, You have fire another select statement to get total number of rows. My suggestion will be, to keep both queries (pagination and total record) in single stored procedure instead of two database call.&lt;/p&gt;
&lt;p&gt;Something like this:&lt;/p&gt;
&lt;p&gt;CREATE PROCEDURE [dbo].[usp_GetPagedOrder]&lt;/p&gt;
&lt;p&gt;	@StartPage INT = 1&lt;/p&gt;
&lt;p&gt;AS&lt;/p&gt;
&lt;p&gt;BEGIN&lt;/p&gt;
&lt;p&gt;WITH Ordered AS (&lt;/p&gt;
&lt;p&gt;SELECT ROW_NUMBER() OVER (ORDER BY OrderID) AS RowNumber, OrderID, OrderDate&lt;/p&gt;
&lt;p&gt;FROM Orders)&lt;/p&gt;
&lt;p&gt;SELECT *&lt;/p&gt;
&lt;p&gt;FROM Ordered&lt;/p&gt;
&lt;p&gt;WHERE RowNumber between @StartPage and @StartPage+9&lt;/p&gt;
&lt;p&gt;SELECT COUNT(*)&lt;/p&gt;
&lt;p&gt;FROM Orders&lt;/p&gt;
&lt;p&gt;END&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5786172" width="1" height="1"&gt;</description></item><item><title>re: SQL Server 2005 - Data Paging</title><link>http://weblogs.asp.net/firoz/archive/2005/06/12/411949.aspx#5785699</link><pubDate>Thu, 14 Feb 2008 14:39:18 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5785699</guid><dc:creator>AT</dc:creator><author>AT</author><description>&lt;p&gt;how to get the total number of rows .. say the actually result set has 500 rows.. &lt;/p&gt;
&lt;p&gt;I want to display the total number of rows to the user&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5785699" width="1" height="1"&gt;</description></item><item><title>re: SQL Server 2005 - Data Paging</title><link>http://weblogs.asp.net/firoz/archive/2005/06/12/411949.aspx#5698403</link><pubDate>Sun, 03 Feb 2008 07:06:37 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5698403</guid><dc:creator>Bernard</dc:creator><author>Bernard</author><description>&lt;p&gt;Thx a lot man... your article help me pagination in sql server 2005&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5698403" width="1" height="1"&gt;</description></item><item><title>re: SQL Server 2005 - Data Paging</title><link>http://weblogs.asp.net/firoz/archive/2005/06/12/411949.aspx#4474060</link><pubDate>Sun, 07 Oct 2007 19:00:37 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:4474060</guid><dc:creator>Ramesh</dc:creator><author>Ramesh</author><description>&lt;p&gt;I think we can use &amp;quot; select top &amp;quot; &amp;nbsp;keyword to minimize the risk of timing out issue &amp;nbsp;for &amp;nbsp;the above scenario.&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=4474060" width="1" height="1"&gt;</description></item><item><title>re: SQL Server 2005 - Data Paging</title><link>http://weblogs.asp.net/firoz/archive/2005/06/12/411949.aspx#3818381</link><pubDate>Mon, 10 Sep 2007 21:07:06 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:3818381</guid><dc:creator>Deus</dc:creator><author>Deus</author><description>&lt;p&gt;We are trying this kind of paging with 1 million of records, it works fine for the first pages, but if the user wants to go to the lastest pages we are getting timeout because the query is taking more than 2 seconds for finishing. This is happening because according to the script the SQL Server is scanning all the records from @StartRecord to @EndRecord.&lt;/p&gt;
&lt;p&gt;I know this is an uncommon case, but we have to support it.&lt;/p&gt;
&lt;p&gt;Do you have any idea to support this case? Does anybody else has proved this kind of paging with 1 millon of records?&lt;/p&gt;
&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=3818381" width="1" height="1"&gt;</description></item></channel></rss>