<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>All About Unidev &#187; Sql Server</title>
	<atom:link href="http://blog.unidev.com/index.php/tag/sql-server/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.unidev.com</link>
	<description>Custom Software, Website and Mobile Development News</description>
	<lastBuildDate>Wed, 25 Jan 2012 20:54:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Paging the query result</title>
		<link>http://blog.unidev.com/index.php/2009/05/01/paging-the-query-result/</link>
		<comments>http://blog.unidev.com/index.php/2009/05/01/paging-the-query-result/#comments</comments>
		<pubDate>Fri, 01 May 2009 21:00:19 +0000</pubDate>
		<dc:creator>George Zheng</dc:creator>
				<category><![CDATA[General Technology]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Sql Server]]></category>

		<guid isPermaLink="false">http://blog.unidev.com/?p=104</guid>
		<description><![CDATA[If you have 5000 products in database, you may like to show them in pages on the screen. It&#8217;s a common request to only retrieve the records on a certain page. This query must be quick and efficient if we are talking about millions of records in a table for an AJAX control. In Oracle, [...]]]></description>
			<content:encoded><![CDATA[<p>If you have 5000 products in database, you may like to show them in pages on the screen. It&#8217;s a common request to only retrieve the records on a certain page. This query must be quick and efficient if we are talking about millions of records in a table for an AJAX control.</p>
<p>In Oracle, we can do this:</p>
<p><code><span style="color: #0000ff;">SELECT * from<br />
(<br />
SELECT *, ROWNUM as rownumber from Product order by name<br />
)<br />
where rownumber between (PageNbr - 1)*PageSize + 1 and PageNbr*PageSize</span></code></p>
<p>As you can see, this query needs to fetch all rows first then narrows down the result. A more efficient query is:</p>
<p><code><span style="color: #0000ff;">select *<br />
from ( select a.*, rownum rnum<br />
from ( select * from product order by name) a<br />
where rownum &lt;= PageNbr*PageSize)<br />
where rnum &gt;= (PageNbr - 1)*PageSize + 1</span></code></p>
<p>Oracle will optimize this query, so it is not as resource intensive as the first one.</p>
<p>In SQL Server 2005, we can do similar query:</p>
<p><code><span style="color: #0000ff;">WITH Temp AS<br />
(<br />
SELECT row_number() OVER (ORDER BY [name]) AS rowNum, * from Product<br />
)<br />
SELECT * FROM Temp<br />
WHERE rowNum between (@PageNbr - 1) * @PageSize + 1 and @PageNbr * @PageSize<br />
ORDER BY [name]</span></code></p>
<p>The query had been optimized in SQL Server as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.unidev.com/index.php/2009/05/01/paging-the-query-result/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

