﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by Bernabe Diaz  / Split by a row (count) number of a query results  / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Wed, 19 Jun 2013 15:45:24 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Split by a row (count) number of a query results</title><link>http://www.sqlservercentral.com/Forums/Topic707076-1509-1.aspx</link><description>Here's the ultimate method.I tried to create an example using the identity column but it required additional queries which wasn't so efficient.  Still, it was more efficient than creating temp tables.Here's a method that works with SQL 2005 using the row_number() function.  I tested it and it works flawlessly.  Feedback would be appreciated.[hr]DECLARE @numberofitemsperpage INTDECLARE @numberofpages INTDECLARE @currentpage int--change the following two variables to your requirementsSET @numberofitemsperpage = 10SET @numberofpages = 5SET @currentpage =0WHILE @currentpage &lt; @numberofpages BEGIN   SELECT a.* FROM    (SELECT row_number() OVER (ORDER BY [b]whatevercolumnyouwant[/b]) AS ROW, *	FROM 		[b]yourtablenamehere[/b]) aWHERE ROW &gt;= @currentpage * @numberofitemsperpage +1 AND Row &lt;= (@currentpage+1) * @numberofitemsperpage	IF @@ROWCOUNT = 0 BREAK	SET @currentpage = @currentpage +1END</description><pubDate>Tue, 26 May 2009 04:24:16 GMT</pubDate><dc:creator>cengland0</dc:creator></item><item><title>RE: Split by a row (count) number of a query results</title><link>http://www.sqlservercentral.com/Forums/Topic707076-1509-1.aspx</link><description>... or by parameterizing the top statement maybe...[code]declare @i intset @i = 1000select top(@i) * from tablename  order by ID[/code]</description><pubDate>Tue, 26 May 2009 01:14:01 GMT</pubDate><dc:creator>b4ndt</dc:creator></item><item><title>RE: Split by a row (count) number of a query results</title><link>http://www.sqlservercentral.com/Forums/Topic707076-1509-1.aspx</link><description>If I'm not mistaken, since this uses the identity column, it might not always produce the same number of columns.  For example, if you deleted a couple rows in the middle, it would still think those rows were there using your between statement.Why not use top?  For example, if you want chucks of 1000 records:select top 1000 * from a_table order by IDYou can then capture the last identity that was selected and your next statement would be:select top 1000 * from a_table where ID &gt; @lastident order by ID</description><pubDate>Fri, 22 May 2009 08:39:34 GMT</pubDate><dc:creator>cengland0</dc:creator></item><item><title>Split by a row (count) number of a query results </title><link>http://www.sqlservercentral.com/Forums/Topic707076-1509-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/scripts/T-SQL/66788/"&gt;Split by a row (count) number of a query results &lt;/A&gt;[/B]</description><pubDate>Wed, 29 Apr 2009 11:20:04 GMT</pubDate><dc:creator>diaz.bernabe</dc:creator></item></channel></rss>