<?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>Tech News, Tech Resources, Technology Articles, Gadget News, Computer News &#187; SQL Server 2005</title>
	<atom:link href="http://www.comteken.com/tag/sql-server-2005/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.comteken.com</link>
	<description>IT news, software technology, IT resources, computer software, laptops, desktops,  information systems,  hardware technology, multimedia, Windows OS, linux clients, network solution, easy internet, essential guides, domains, webhosting, web program, database programming</description>
	<lastBuildDate>Fri, 27 Jan 2012 11:22:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Pagination In SQL Server 2005</title>
		<link>http://www.comteken.com/database-programming/pagination-in-sql-server-2005/</link>
		<comments>http://www.comteken.com/database-programming/pagination-in-sql-server-2005/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 16:08:36 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Database Programming]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Server 2005]]></category>

		<guid isPermaLink="false">http://www.comteken.com/?p=40</guid>
		<description><![CDATA[<a href="http://www.comteken.com/database-programming/pagination-in-sql-server-2005/"><img align="left" hspace="5" width="100" src="http://www.singingeels.com/Articles/UserImage.aspx?ImageID=4ee676e0-26a2-487e-a8f0-daa2202944bb" class="alignleft wp-post-image tfe" alt="SQL results for " title="SQL results for " /></a>If you&#8217;ve used other flavors of SQL (such as MySQL or PostgreSQL) and now you have switched over to TSQL (Microsoft SQL Server) you may have noticed that there is no native way of doing pagination. In some SQL versions, pagination was as easy as &#8220;SELECT * FROM &#8216;myTable&#8217; LIMIT 0, 10&#8243; and that would [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve used other flavors of SQL (such as MySQL or PostgreSQL) and now you have switched over to TSQL (Microsoft SQL Server) you may have noticed that there is no native way of doing pagination. In some SQL versions, pagination was as easy as &#8220;SELECT * FROM &#8216;myTable&#8217; LIMIT 0, 10&#8243; and that would pull out the first 10 records from your table. SQL Server 2005 introduced a beautiful new world of &#8216;ranking functions&#8217;, one of which we can use to easily perform pagination directly in SQL.</p>
<p>While these other flavors of SQL implimented pagination only for the sake of pagination &#8211; meaning the absolutely only reason for the functions is to limit results sets into &#8216;pages&#8217; of data &#8211; SQL Server 2005&#8242;s ranking functions provide a lot of power and information. However, for this article, we are only going to demonstrate how to use these abilities for the sake of pagination.</p>
<h3>RowNumber In SQL</h3>
<p>First of all, we&#8217;ll need to get the &#8220;row number&#8221; from a result set. This is the basis of pagination because you need to know what row you are on to begin demanding only a subset from SQL. First we&#8217;ll do just a basic query and look at the results:</p>
<div><span>SELECT</span><br />
*<br />
<span>FROM</span><br />
dbo.Customers<br />
<span>ORDER</span> <span>BY</span><br />
LastName, FirstName</div>
<p>This query above will display the following results:</p>
<p><img title="SQL results for 'customer' table without pagination" src="http://www.singingeels.com/Articles/UserImage.aspx?ImageID=4ee676e0-26a2-487e-a8f0-daa2202944bb" alt="SQL results for 'customer' table without pagination" /></p>
<p>Now we&#8217;ll make a slight modification to add a &#8220;RowNumber&#8221; field.</p>
<div><span>SELECT</span><br />
ROW_NUMBER() <span>OVER</span> (<span>ORDER</span> <span>BY</span> LastName, FirstName) <span>AS</span> RowNumber,<br />
*<br />
<span>FROM</span><br />
dbo.Customers</div>
<p>This syntax may look a little odd, but it&#8217;s not that difficult. First of all, the ROW_NUMBER() function (just like all other &#8216;ranking functions&#8217; in SQL SERVER 2005) needs an OVER clause. Notice that we moved the ORDER BY clause inside the OVER clause. Basically what this is doing is telling SQL how we want the ROW_NUMBER() function to number each record. So we are telling SQL to number the records 1, 2, 3, 4 in the order of LastName then FirstName. Here are the results:</p>
<p><img title="SQL results for 'customer' table with pagination" src="http://www.singingeels.com/Articles/UserImage.aspx?ImageID=89cab536-9790-4edd-a3f0-9aa243104ddc" alt="SQL results for 'customer' table with pagination" /></p>
<p>So now that we have a &#8220;RowNumber&#8221; field, we can add a simple WHERE clause to get only a limited number of records. We will have to wrap query in another SELECT statment so that we can use all of our dynamic columns (such as our RowNumber column) by their names that we assigned them. Also, I&#8217;m going to use the TOP clause to limit the query to only return 2 records at a time. Lets see the query, and the results:</p>
<div><span>SELECT</span> <span>TOP</span> 2 * <span>FROM</span> (<span>SELECT</span><br />
ROW_NUMBER() <span>OVER</span> (<span>ORDER</span> <span>BY</span> LastName, FirstName) <span>AS</span> RowNumber,<br />
*<br />
<span>FROM</span><br />
dbo.Customers) _myResults<br />
<span>WHERE</span><br />
RowNumber &gt; 2</div>
<p>Notice I &#8216;aliased&#8217; the first query and called it &#8220;_myResults&#8221;. This basically treats that inner query as a VIEW so that I could pull out that first column by it&#8217;s name, RowNumber. Here are the results:</p>
<p><img title="Paginated SQL results for 'customer' table" src="http://www.singingeels.com/Articles/UserImage.aspx?ImageID=81d89626-1ba8-424d-965a-fea3eccf29be" alt="Paginated SQL results for 'customer' table" /></p>
<h3>Conclusion</h3>
<p>Though it may seem like you have to do a lot more work in order to get pagination in SQL Server 2005, the pay-off is that not only does the code make sense, but you gain a lot more power with the ranking functions TSQL. Try playing around with the parameters to see how they affect your queries and what you can do with them.</p>
<p>Source:singingeels.com</p>



Share and Enjoy:


	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fpagination-in-sql-server-2005%2F&amp;title=Pagination%20In%20SQL%20Server%202005&amp;bodytext=If%20you%27ve%20used%20other%20flavors%20of%20SQL%20%28such%20as%20MySQL%20or%20PostgreSQL%29%20and%20now%20you%20have%20switched%20over%20to%20TSQL%20%28Microsoft%20SQL%20Server%29%20you%20may%20have%20noticed%20that%20there%20is%20no%20native%20way%20of%20doing%20pagination.%20In%20some%20SQL%20versions%2C%20pagination%20was%20as%20easy%20as%20%22SEL" title="Digg"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" /></a>
	<a rel="nofollow"  target="_blank" href="http://sphinn.com/index.php?c=post&amp;m=submit&amp;link=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fpagination-in-sql-server-2005%2F" title="Sphinn"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/sphinn.png" title="Sphinn" alt="Sphinn" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fpagination-in-sql-server-2005%2F&amp;title=Pagination%20In%20SQL%20Server%202005&amp;notes=If%20you%27ve%20used%20other%20flavors%20of%20SQL%20%28such%20as%20MySQL%20or%20PostgreSQL%29%20and%20now%20you%20have%20switched%20over%20to%20TSQL%20%28Microsoft%20SQL%20Server%29%20you%20may%20have%20noticed%20that%20there%20is%20no%20native%20way%20of%20doing%20pagination.%20In%20some%20SQL%20versions%2C%20pagination%20was%20as%20easy%20as%20%22SEL" title="del.icio.us"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fpagination-in-sql-server-2005%2F&amp;t=Pagination%20In%20SQL%20Server%202005" title="Facebook"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mixx.com/submit?page_url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fpagination-in-sql-server-2005%2F&amp;title=Pagination%20In%20SQL%20Server%202005" title="Mixx"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/mixx.png" title="Mixx" alt="Mixx" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fpagination-in-sql-server-2005%2F&amp;title=Pagination%20In%20SQL%20Server%202005&amp;annotation=If%20you%27ve%20used%20other%20flavors%20of%20SQL%20%28such%20as%20MySQL%20or%20PostgreSQL%29%20and%20now%20you%20have%20switched%20over%20to%20TSQL%20%28Microsoft%20SQL%20Server%29%20you%20may%20have%20noticed%20that%20there%20is%20no%20native%20way%20of%20doing%20pagination.%20In%20some%20SQL%20versions%2C%20pagination%20was%20as%20easy%20as%20%22SEL" title="Google Bookmarks"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.blinklist.com/index.php?Action=Blink/addblink.php&amp;Url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fpagination-in-sql-server-2005%2F&amp;Title=Pagination%20In%20SQL%20Server%202005" title="BlinkList"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/blinklist.png" title="BlinkList" alt="BlinkList" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.diigo.com/post?url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fpagination-in-sql-server-2005%2F&amp;title=Pagination%20In%20SQL%20Server%202005" title="Diigo"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/diigo.png" title="Diigo" alt="Diigo" /></a>
	<a rel="nofollow"  target="_blank" href="http://cgi.fark.com/cgi/fark/farkit.pl?h=Pagination%20In%20SQL%20Server%202005&amp;u=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fpagination-in-sql-server-2005%2F" title="Fark"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/fark.png" title="Fark" alt="Fark" /></a>
	<a rel="nofollow"  target="_blank" href="http://faves.com/Authoring.aspx?u=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fpagination-in-sql-server-2005%2F&amp;title=Pagination%20In%20SQL%20Server%202005" title="Faves"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/bluedot.png" title="Faves" alt="Faves" /></a>
	<a rel="nofollow"  target="_blank" href="http://laaik.it/NewStoryCompact.aspx?uri=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fpagination-in-sql-server-2005%2F&amp;headline=Pagination%20In%20SQL%20Server%202005&amp;cat=5e082fcc-8a3b-47e2-acec-fdf64ff19d12" title="laaik.it"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/laaikit.png" title="laaik.it" alt="laaik.it" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fpagination-in-sql-server-2005%2F&amp;title=Pagination%20In%20SQL%20Server%202005&amp;source=Tech+News%2C+Tech+Resources%2C+Technology+Articles%2C+Gadget+News%2C+Computer+News+IT+news%2C+software+technology%2C+IT+resources%2C+computer+software%2C+laptops%2C+desktops%2C++information+systems%2C++hardware+technology%2C+multimedia%2C+Windows+OS%2C+linux+clients%2C+network+solution%2C+easy+internet%2C+essential+guides%2C+domains%2C+webhosting%2C+web+program%2C+database+programming&amp;summary=If%20you%27ve%20used%20other%20flavors%20of%20SQL%20%28such%20as%20MySQL%20or%20PostgreSQL%29%20and%20now%20you%20have%20switched%20over%20to%20TSQL%20%28Microsoft%20SQL%20Server%29%20you%20may%20have%20noticed%20that%20there%20is%20no%20native%20way%20of%20doing%20pagination.%20In%20some%20SQL%20versions%2C%20pagination%20was%20as%20easy%20as%20%22SEL" title="LinkedIn"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" /></a>
	<a rel="nofollow"  target="_blank" href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fpagination-in-sql-server-2005%2F&amp;title=Pagination%20In%20SQL%20Server%202005" title="Live"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mister-wong.com/addurl/?bm_url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fpagination-in-sql-server-2005%2F&amp;bm_description=Pagination%20In%20SQL%20Server%202005&amp;plugin=soc" title="MisterWong"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/misterwong.png" title="MisterWong" alt="MisterWong" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fpagination-in-sql-server-2005%2F&amp;t=Pagination%20In%20SQL%20Server%202005" title="MySpace"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.netvibes.com/share?title=Pagination%20In%20SQL%20Server%202005&amp;url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fpagination-in-sql-server-2005%2F" title="Netvibes"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.netvouz.com/action/submitBookmark?url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fpagination-in-sql-server-2005%2F&amp;title=Pagination%20In%20SQL%20Server%202005&amp;popup=no" title="Netvouz"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/netvouz.png" title="Netvouz" alt="Netvouz" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.newsvine.com/_tools/seed&amp;save?u=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fpagination-in-sql-server-2005%2F&amp;h=Pagination%20In%20SQL%20Server%202005" title="NewsVine"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/newsvine.png" title="NewsVine" alt="NewsVine" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.propeller.com/submit/?url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fpagination-in-sql-server-2005%2F" title="Propeller"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/propeller.png" title="Propeller" alt="Propeller" /></a>
	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fpagination-in-sql-server-2005%2F&amp;title=Pagination%20In%20SQL%20Server%202005" title="Reddit"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" /></a>
	<a rel="nofollow"  target="_blank" href="http://slashdot.org/bookmark.pl?title=Pagination%20In%20SQL%20Server%202005&amp;url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fpagination-in-sql-server-2005%2F" title="Slashdot"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/slashdot.png" title="Slashdot" alt="Slashdot" /></a>
	<a rel="nofollow"  target="_blank" href="http://socialogs.com/add_story.php?story_url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fpagination-in-sql-server-2005%2F&amp;story_title=Pagination%20In%20SQL%20Server%202005" title="Socialogs"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/socialogs.png" title="Socialogs" alt="Socialogs" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fpagination-in-sql-server-2005%2F&amp;title=Pagination%20In%20SQL%20Server%202005" title="StumbleUpon"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" /></a>
	<a rel="nofollow"  target="_blank" href="http://technorati.com/faves?add=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fpagination-in-sql-server-2005%2F" title="Technorati"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=Pagination%20In%20SQL%20Server%202005%20-%20http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fpagination-in-sql-server-2005%2F" title="Twitter"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" /></a>
	<a rel="nofollow"  target="_blank" href="http://buzz.yahoo.com/submit/?submitUrl=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fpagination-in-sql-server-2005%2F&amp;submitHeadline=Pagination%20In%20SQL%20Server%202005&amp;submitSummary=If%20you%27ve%20used%20other%20flavors%20of%20SQL%20%28such%20as%20MySQL%20or%20PostgreSQL%29%20and%20now%20you%20have%20switched%20over%20to%20TSQL%20%28Microsoft%20SQL%20Server%29%20you%20may%20have%20noticed%20that%20there%20is%20no%20native%20way%20of%20doing%20pagination.%20In%20some%20SQL%20versions%2C%20pagination%20was%20as%20easy%20as%20%22SEL&amp;submitCategory=science&amp;submitAssetType=text" title="Yahoo! Buzz"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/yahoobuzz.png" title="Yahoo! Buzz" alt="Yahoo! Buzz" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.comteken.com/database-programming/pagination-in-sql-server-2005/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

