<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Easy way to page data in Sql Server</title>
	<atom:link href="http://benreichelt.net/blog/2006/01/26/easy-way-to-page-data-in-sql-server/feed/" rel="self" type="application/rss+xml" />
	<link>http://benreichelt.net/blog/2006/01/26/easy-way-to-page-data-in-sql-server/</link>
	<description></description>
	<lastBuildDate>Tue, 18 Aug 2009 22:20:45 -0400</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Hieu</title>
		<link>http://benreichelt.net/blog/2006/01/26/easy-way-to-page-data-in-sql-server/comment-page-1/#comment-1154</link>
		<dc:creator>Hieu</dc:creator>
		<pubDate>Tue, 16 Jun 2009 01:40:34 +0000</pubDate>
		<guid isPermaLink="false">http://s194721765.onlinehome.us/blog/2006/01/26/easy-way-to-page-data-in-sql-server/#comment-1154</guid>
		<description>If table have large data, &quot;ROW_NUMBER() OVER (ORDER BY colname)&quot; is very slow.</description>
		<content:encoded><![CDATA[<p>If table have large data, &#8220;ROW_NUMBER() OVER (ORDER BY colname)&#8221; is very slow.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Maxi</title>
		<link>http://benreichelt.net/blog/2006/01/26/easy-way-to-page-data-in-sql-server/comment-page-1/#comment-1138</link>
		<dc:creator>Maxi</dc:creator>
		<pubDate>Mon, 23 Feb 2009 07:47:27 +0000</pubDate>
		<guid isPermaLink="false">http://s194721765.onlinehome.us/blog/2006/01/26/easy-way-to-page-data-in-sql-server/#comment-1138</guid>
		<description>I have been waiting for this feature too!</description>
		<content:encoded><![CDATA[<p>I have been waiting for this feature too!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jayan</title>
		<link>http://benreichelt.net/blog/2006/01/26/easy-way-to-page-data-in-sql-server/comment-page-1/#comment-1136</link>
		<dc:creator>Jayan</dc:creator>
		<pubDate>Fri, 30 Jan 2009 04:25:35 +0000</pubDate>
		<guid isPermaLink="false">http://s194721765.onlinehome.us/blog/2006/01/26/easy-way-to-page-data-in-sql-server/#comment-1136</guid>
		<description>Roopa,

ROW_NUMBER() column can&#039;t refer in where clause


please try this Script

--- Code 1
select * from (
select Enty_inid ‘StoreID’, Enty_vcName ‘StoreName’ , 
ROW_NUMBER() OVER (order by Enty_inSalesRank desc ) as ‘RowNumber’ 
from tb_entity )a
where RowNumber between 1 and 10

---code 2
WITH actualSet
(
select Enty_inid ‘StoreID’, Enty_vcName ‘StoreName’ , 
ROW_NUMBER() OVER (order by Enty_inSalesRank desc ) as ‘RowNumber’ 
from tb_entity 
)

SELECT * FROM actualSet where RowNumber between 1 and 10</description>
		<content:encoded><![CDATA[<p>Roopa,</p>
<p>ROW_NUMBER() column can&#8217;t refer in where clause</p>
<p>please try this Script</p>
<p>&#8212; Code 1<br />
select * from (<br />
select Enty_inid ‘StoreID’, Enty_vcName ‘StoreName’ ,<br />
ROW_NUMBER() OVER (order by Enty_inSalesRank desc ) as ‘RowNumber’<br />
from tb_entity )a<br />
where RowNumber between 1 and 10</p>
<p>&#8212;code 2<br />
WITH actualSet<br />
(<br />
select Enty_inid ‘StoreID’, Enty_vcName ‘StoreName’ ,<br />
ROW_NUMBER() OVER (order by Enty_inSalesRank desc ) as ‘RowNumber’<br />
from tb_entity<br />
)</p>
<p>SELECT * FROM actualSet where RowNumber between 1 and 10</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roopa</title>
		<link>http://benreichelt.net/blog/2006/01/26/easy-way-to-page-data-in-sql-server/comment-page-1/#comment-642</link>
		<dc:creator>Roopa</dc:creator>
		<pubDate>Wed, 28 Feb 2007 10:37:39 +0000</pubDate>
		<guid isPermaLink="false">http://s194721765.onlinehome.us/blog/2006/01/26/easy-way-to-page-data-in-sql-server/#comment-642</guid>
		<description>me too, got the Error

Msg 207, Level 16, State 1, Line 5
Invalid column name &#039;RowNumber&#039;.


Here is my query
 select Enty_inid &#039;StoreID&#039;, Enty_vcName &#039;StoreName&#039; ,  ROW_NUMBER() OVER (order by  Enty_inSalesRank desc  ) as &#039;RowNumber&#039;  from tb_entity and RowNumber between          1 and         10</description>
		<content:encoded><![CDATA[<p>me too, got the Error</p>
<p>Msg 207, Level 16, State 1, Line 5<br />
Invalid column name &#8216;RowNumber&#8217;.</p>
<p>Here is my query<br />
 select Enty_inid &#8216;StoreID&#8217;, Enty_vcName &#8216;StoreName&#8217; ,  ROW_NUMBER() OVER (order by  Enty_inSalesRank desc  ) as &#8216;RowNumber&#8217;  from tb_entity and RowNumber between          1 and         10</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: breichelt</title>
		<link>http://benreichelt.net/blog/2006/01/26/easy-way-to-page-data-in-sql-server/comment-page-1/#comment-641</link>
		<dc:creator>breichelt</dc:creator>
		<pubDate>Tue, 07 Feb 2006 09:03:29 +0000</pubDate>
		<guid isPermaLink="false">http://s194721765.onlinehome.us/blog/2006/01/26/easy-way-to-page-data-in-sql-server/#comment-641</guid>
		<description>carter, I agree that LIMIT is very simple and easy to use, but I think that Row_Number could be used for some interesting scenarios, because you can set th Row_Number based on a field, and then order the actual result set by a different column. I can&#039;t think of anything offhand that you would use that for, but the fact that its there leaves open the possibility.</description>
		<content:encoded><![CDATA[<p>carter, I agree that LIMIT is very simple and easy to use, but I think that Row_Number could be used for some interesting scenarios, because you can set th Row_Number based on a field, and then order the actual result set by a different column. I can&#8217;t think of anything offhand that you would use that for, but the fact that its there leaves open the possibility.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://benreichelt.net/blog/2006/01/26/easy-way-to-page-data-in-sql-server/comment-page-1/#comment-640</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Tue, 07 Feb 2006 00:19:45 +0000</pubDate>
		<guid isPermaLink="false">http://s194721765.onlinehome.us/blog/2006/01/26/easy-way-to-page-data-in-sql-server/#comment-640</guid>
		<description>Nice feature, but it still seems more complicated than it needs to be. LIMIT works beautifully as is. I guess there is some other crazy use for the resulting Row Number column that I&#039;m not considering?...</description>
		<content:encoded><![CDATA[<p>Nice feature, but it still seems more complicated than it needs to be. LIMIT works beautifully as is. I guess there is some other crazy use for the resulting Row Number column that I&#8217;m not considering?&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: breichelt</title>
		<link>http://benreichelt.net/blog/2006/01/26/easy-way-to-page-data-in-sql-server/comment-page-1/#comment-639</link>
		<dc:creator>breichelt</dc:creator>
		<pubDate>Mon, 30 Jan 2006 15:06:55 +0000</pubDate>
		<guid isPermaLink="false">http://s194721765.onlinehome.us/blog/2006/01/26/easy-way-to-page-data-in-sql-server/#comment-639</guid>
		<description>Frans, you clearly know more about the feature than I do, thanks for the insight :)</description>
		<content:encoded><![CDATA[<p>Frans, you clearly know more about the feature than I do, thanks for the insight <img src='http://benreichelt.net/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: breichelt</title>
		<link>http://benreichelt.net/blog/2006/01/26/easy-way-to-page-data-in-sql-server/comment-page-1/#comment-638</link>
		<dc:creator>breichelt</dc:creator>
		<pubDate>Mon, 30 Jan 2006 15:06:24 +0000</pubDate>
		<guid isPermaLink="false">http://s194721765.onlinehome.us/blog/2006/01/26/easy-way-to-page-data-in-sql-server/#comment-638</guid>
		<description>Damon, I&#039;m not sure what&#039;s causing the problem that you&#039;re having, I would make sure that you are including the &#039;AS RowNumber&#039; in the select statement.</description>
		<content:encoded><![CDATA[<p>Damon, I&#8217;m not sure what&#8217;s causing the problem that you&#8217;re having, I would make sure that you are including the &#8216;AS RowNumber&#8217; in the select statement.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://benreichelt.net/blog/2006/01/26/easy-way-to-page-data-in-sql-server/comment-page-1/#comment-637</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Thu, 26 Jan 2006 15:20:20 +0000</pubDate>
		<guid isPermaLink="false">http://s194721765.onlinehome.us/blog/2006/01/26/easy-way-to-page-data-in-sql-server/#comment-637</guid>
		<description>You don&#039;t have to specify tht ORDER BY FirstName at the end of the query. It&#039;s important that if you write paging code like this, you formulate your complete ORDER BY clause in the OVER clause for ROW_NUMBER().

Why is this important? Because the ORDER BY in the select will be executed AFTER the row_number() function has been applied. This means that the ROW_NUMBER() column doesn&#039;t have to be 1, 2, 3, 4, 5 etc.

You should therefore use these rules of thumb:
- place the SELECT&#039;s ORDER BY clause in the ROW_NUMBER&#039;s OVER clause
- instead add an ORDER BY clause to the SELECT to sort the ROW_NUMBER column ASC. This is necessary because by default a SELECT isn&#039;t guaranteed to be sequential, it can be that the rows aren&#039;t in the order in which you would expect. This is by definition of SELECT.

So then, use a WITH statement to construct a CTE:
WITH __actualSet
(
SELECT C.*, ROW_NUMBER() OVER (ORDER BY CompanyName ASC) AS RowNumber FROM Customers C
ORDER BY RowNumber ASC
) SELECT * FROM __actualSet WHERE RowNumber BETWEEN 1 AND 10</description>
		<content:encoded><![CDATA[<p>You don&#8217;t have to specify tht ORDER BY FirstName at the end of the query. It&#8217;s important that if you write paging code like this, you formulate your complete ORDER BY clause in the OVER clause for ROW_NUMBER().</p>
<p>Why is this important? Because the ORDER BY in the select will be executed AFTER the row_number() function has been applied. This means that the ROW_NUMBER() column doesn&#8217;t have to be 1, 2, 3, 4, 5 etc.</p>
<p>You should therefore use these rules of thumb:<br />
- place the SELECT&#8217;s ORDER BY clause in the ROW_NUMBER&#8217;s OVER clause<br />
- instead add an ORDER BY clause to the SELECT to sort the ROW_NUMBER column ASC. This is necessary because by default a SELECT isn&#8217;t guaranteed to be sequential, it can be that the rows aren&#8217;t in the order in which you would expect. This is by definition of SELECT.</p>
<p>So then, use a WITH statement to construct a CTE:<br />
WITH __actualSet<br />
(<br />
SELECT C.*, ROW_NUMBER() OVER (ORDER BY CompanyName ASC) AS RowNumber FROM Customers C<br />
ORDER BY RowNumber ASC<br />
) SELECT * FROM __actualSet WHERE RowNumber BETWEEN 1 AND 10</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://benreichelt.net/blog/2006/01/26/easy-way-to-page-data-in-sql-server/comment-page-1/#comment-636</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Thu, 26 Jan 2006 15:13:31 +0000</pubDate>
		<guid isPermaLink="false">http://s194721765.onlinehome.us/blog/2006/01/26/easy-way-to-page-data-in-sql-server/#comment-636</guid>
		<description>Thanks to Ben Reichelt over at CodeBetter.com blogs for pointing out the new Row_Number() function in...</description>
		<content:encoded><![CDATA[<p>Thanks to Ben Reichelt over at CodeBetter.com blogs for pointing out the new Row_Number() function in&#8230;</p>
]]></content:encoded>
	</item>
</channel>
</rss>
