<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://www2.sqlblog.com/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>Paging in SQL Server 2005</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/paging-in-sql-server-2005.aspx</link><description>I keep seeing questions on newsgroups about paging in stored procedures, and whether there will be a better way in SQL Server 2005. However, aside from a few answers in newsgroups, I haven't seen any content on how to do it. So I'd like to spend a few</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>re: Paging in SQL Server 2005</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/paging-in-sql-server-2005.aspx#13614</link><pubDate>Tue, 28 Apr 2009 20:59:33 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:13614</guid><dc:creator>max</dc:creator><description>&lt;p&gt;Great article. Thanks&lt;/p&gt;
</description></item><item><title>re: Paging in SQL Server 2005</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/paging-in-sql-server-2005.aspx#27042</link><pubDate>Wed, 14 Jul 2010 15:48:16 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:27042</guid><dc:creator>daryl2000</dc:creator><description>&lt;p&gt;Thank you for posting this article, which greatly facilitated the development of a paging sproc. &amp;nbsp;While it was easy to incorporate the ROW_NUMBER() function, SQL 2005 did not allow me to use a variable with TOP or a order by clause in the subquery. &amp;nbsp;I tried to use a variable with SET ROWCOUNT in lieu of TOP, but the results were erroneous (due to the lack of the order by clause in the subquery). &amp;nbsp;Based upon the query in the article, the paging sproc would be:&lt;/p&gt;
&lt;p&gt;CREATE PROCEDURE dbo.PagedResults&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;@PAGE_SIZE bigint,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;@PAGE_NUM bigint&lt;/p&gt;
&lt;p&gt;AS&lt;/p&gt;
&lt;p&gt;BEGIN&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;SET NOCOUNT ON;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;SELECT x.SomewhatLargeString&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;FROM (&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;SELECT A.SomewhatLargeString, ROW_NUMBER() OVER(ORDER BY A.SomewhatLargeString) AS TheCount&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;FROM #BigTableOfStrings A&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;) x&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;WHERE x.TheCount &amp;gt; ((@PAGE_NUM - 1) * @PAGE_SIZE) AND x.TheCount &amp;lt;= (@PAGE_NUM * @PAGE_SIZE)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;ORDER BY x.TheCount&lt;/p&gt;
&lt;p&gt;END&lt;/p&gt;
&lt;p&gt;Without the use of dynamic SQL, could the sproc be tweaked to improve performance? &amp;nbsp;Thanks.&lt;/p&gt;
</description></item><item><title>re: Paging in SQL Server 2005</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2006/07/12/paging-in-sql-server-2005.aspx#27050</link><pubDate>Wed, 14 Jul 2010 20:14:56 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:27050</guid><dc:creator>Adam Machanic</dc:creator><description>&lt;p&gt;Hi Daryl2000,&lt;/p&gt;
&lt;p&gt;There's no reason you shouldn't be able to use TOP + ORDER BY in a derived table. That's been allowed for several versions (at least since 7.0). Not sure what's going on there. However, in the time between when I originally wrote the post and now I've played a lot with this technique and have seen no differences between performance with and without the TOP + ORDER BY -- so it's not needed in any case.&lt;/p&gt;
&lt;p&gt;Your query looks good to me as-is. I don't think there is any improvement to be had.&lt;/p&gt;
</description></item></channel></rss>