<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: SQL SERVER &#8211; 2005 T-SQL Paging Query Technique Comparison (OVER and ROW_NUMBER()) &#8211; CTE vs. Derived Table</title>
	<atom:link href="http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/</link>
	<description>SQL, SQL Server, MySQL, Big Data and NoSQL</description>
	<lastBuildDate>Tue, 18 Jun 2013 06:30:30 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: SQL SERVER &#8211; Weekly Series &#8211; Memory Lane &#8211; #033 &#124; Journey to SQL Authority with Pinal Dave</title>
		<link>http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-502039</link>
		<dc:creator><![CDATA[SQL SERVER &#8211; Weekly Series &#8211; Memory Lane &#8211; #033 &#124; Journey to SQL Authority with Pinal Dave]]></dc:creator>
		<pubDate>Sat, 15 Jun 2013 01:30:41 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-502039</guid>
		<description><![CDATA[[&#8230;] T-SQL Paging Query Technique Comparison (OVER and ROW_NUMBER()) – CTE vs. Derived Table I have received few emails and comments about my post SQL SERVER – T-SQL Paging Query Technique Comparison – SQL 2000 vs SQL 2005. The main question was is this can be done using CTE? Absolutely! What about Performance? It is identical! Please refer above mentioned article for the history of paging. [&#8230;]]]></description>
		<content:encoded><![CDATA[<p>[&#8230;] T-SQL Paging Query Technique Comparison (OVER and ROW_NUMBER()) – CTE vs. Derived Table I have received few emails and comments about my post SQL SERVER – T-SQL Paging Query Technique Comparison – SQL 2000 vs SQL 2005. The main question was is this can be done using CTE? Absolutely! What about Performance? It is identical! Please refer above mentioned article for the history of paging. [&#8230;]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert</title>
		<link>http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-490913</link>
		<dc:creator><![CDATA[Robert]]></dc:creator>
		<pubDate>Thu, 06 Jun 2013 16:16:45 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-490913</guid>
		<description><![CDATA[I need help also about paging and using UNION ALL for multiple tables:

How do i implement an optimized paging when joining multiple tables using UNION ALL and returning only specific number of rows...
-------------------------------------------------------------------------------------------------------------------
declare @startRow int
declare @PageCount int

set @startRow = 0
set @PageCount = 20

set rowcount @PageCount

select Row_Number() OVER(Order by col1) as RowNumber, col1, col2
from
(
select col1, col2 from table1 where datetimeCol between (@dateFrom and @dateTo)
union all
select col1, col2 from table2 where datetimeCol between (@dateFrom and @dateTo)
union all
select col1, col2 from table3 where datetimeCol between (@dateFrom and @dateTo)
union all
select col1, col2 from table4 where datetimeCol between (@dateFrom and @dateTo)
union all
select col1, col2 from table5 where datetimeCol between (@dateFrom and @dateTo)
) as tmpTable
where RowNumber &gt; @startRow
-------------------------------------------------------------------------------------------------------------------

table 3, 4, &amp; 5 have huge number of row (millions of rows) where table 1 &amp; 2 may only have few thousand rows.

If startRow is &quot;0&quot;, I only expect data from Row 1 to 20 (from Table1).
I&#039;m getting the correct result but has a high overhead on the remaining table while sql server tries to all all the data and filter it....

the longer the interval of the @dateFrom and @dateTo makes my query significantly slower while trying to retrieve only few rows from the overall result set

Please help how i can implement a simple but better approach with a similar logic. :(]]></description>
		<content:encoded><![CDATA[<p>I need help also about paging and using UNION ALL for multiple tables:</p>
<p>How do i implement an optimized paging when joining multiple tables using UNION ALL and returning only specific number of rows&#8230;<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
declare @startRow int<br />
declare @PageCount int</p>
<p>set @startRow = 0<br />
set @PageCount = 20</p>
<p>set rowcount @PageCount</p>
<p>select Row_Number() OVER(Order by col1) as RowNumber, col1, col2<br />
from<br />
(<br />
select col1, col2 from table1 where datetimeCol between (@dateFrom and @dateTo)<br />
union all<br />
select col1, col2 from table2 where datetimeCol between (@dateFrom and @dateTo)<br />
union all<br />
select col1, col2 from table3 where datetimeCol between (@dateFrom and @dateTo)<br />
union all<br />
select col1, col2 from table4 where datetimeCol between (@dateFrom and @dateTo)<br />
union all<br />
select col1, col2 from table5 where datetimeCol between (@dateFrom and @dateTo)<br />
) as tmpTable<br />
where RowNumber &gt; @startRow<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>table 3, 4, &amp; 5 have huge number of row (millions of rows) where table 1 &amp; 2 may only have few thousand rows.</p>
<p>If startRow is &#8220;0&#8243;, I only expect data from Row 1 to 20 (from Table1).<br />
I&#8217;m getting the correct result but has a high overhead on the remaining table while sql server tries to all all the data and filter it&#8230;.</p>
<p>the longer the interval of the @dateFrom and @dateTo makes my query significantly slower while trying to retrieve only few rows from the overall result set</p>
<p>Please help how i can implement a simple but better approach with a similar logic. :(</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mio</title>
		<link>http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-293397</link>
		<dc:creator><![CDATA[mio]]></dc:creator>
		<pubDate>Sun, 03 Jun 2012 18:00:34 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-293397</guid>
		<description><![CDATA[how to do a paging from infinite looping CTE?

for example i have a recurrence schedule without an end date : 

;WITH cteDate as 
(
    (
		select event_id, summary, dt_start
		from  events
	)
    UNION ALL
    (
		select event_id, summary, dateadd(d,1,dt_start)
		from  cteDate
	)
)

SELECT * FROM cteDate
order by dt_start desc
OPTION(MAXRECURSION 0)

Thanks]]></description>
		<content:encoded><![CDATA[<p>how to do a paging from infinite looping CTE?</p>
<p>for example i have a recurrence schedule without an end date : </p>
<p>;WITH cteDate as<br />
(<br />
    (<br />
		select event_id, summary, dt_start<br />
		from  events<br />
	)<br />
    UNION ALL<br />
    (<br />
		select event_id, summary, dateadd(d,1,dt_start)<br />
		from  cteDate<br />
	)<br />
)</p>
<p>SELECT * FROM cteDate<br />
order by dt_start desc<br />
OPTION(MAXRECURSION 0)</p>
<p>Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQL SERVER &#8211; Convert Subquery to CTE &#8211; SQL in Sixty Seconds #001 &#8211; Video &#171; SQL Server Journey with SQL Authority</title>
		<link>http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-249753</link>
		<dc:creator><![CDATA[SQL SERVER &#8211; Convert Subquery to CTE &#8211; SQL in Sixty Seconds #001 &#8211; Video &#171; SQL Server Journey with SQL Authority]]></dc:creator>
		<pubDate>Wed, 08 Feb 2012 01:32:16 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-249753</guid>
		<description><![CDATA[[...] More on CTE: Simple Example of Recursive CTE Multiple CTE in One SELECT Statement Query Common Table Expression (CTE) and Few Observation Delete Duplicate Rows Simple Example of Recursive CTE – Part 2 – MAXRECURSION – Prevent CTE Infinite Loop T-SQL Paging Query Technique Comparison (OVER and ROW_NUMBER()) – CTE vs. Derived Table [...]]]></description>
		<content:encoded><![CDATA[<p>[...] More on CTE: Simple Example of Recursive CTE Multiple CTE in One SELECT Statement Query Common Table Expression (CTE) and Few Observation Delete Duplicate Rows Simple Example of Recursive CTE – Part 2 – MAXRECURSION – Prevent CTE Infinite Loop T-SQL Paging Query Technique Comparison (OVER and ROW_NUMBER()) – CTE vs. Derived Table [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQL SERVER &#8211; Common Gotcha&#8217;s Associated with Common Table Expressions (CTE) &#8211; Quiz &#8211; Puzzle &#8211; 26 of 31 &#171; SQL Server Journey with SQL Authority</title>
		<link>http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-244330</link>
		<dc:creator><![CDATA[SQL SERVER &#8211; Common Gotcha&#8217;s Associated with Common Table Expressions (CTE) &#8211; Quiz &#8211; Puzzle &#8211; 26 of 31 &#171; SQL Server Journey with SQL Authority]]></dc:creator>
		<pubDate>Fri, 27 Jan 2012 01:31:28 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-244330</guid>
		<description><![CDATA[[...] SQL Server Interview Questions and Answers ISBN: 1466405643 Page#109-112 Common Table Expression (CTE) and Few Observation Multiple CTE in One SELECT Statement Query Delete Duplicate Rows Simple Example of Recursive CTE SQL SERVER – Simple Example of Recursive CTE – Part 2 – MAXRECURSION – Prevent CTE Infinite Loop T-SQL Paging Query Technique Comparison (OVER and ROW_NUMBER()) – CTE vs. Derived Table [...]]]></description>
		<content:encoded><![CDATA[<p>[...] SQL Server Interview Questions and Answers ISBN: 1466405643 Page#109-112 Common Table Expression (CTE) and Few Observation Multiple CTE in One SELECT Statement Query Delete Duplicate Rows Simple Example of Recursive CTE SQL SERVER – Simple Example of Recursive CTE – Part 2 – MAXRECURSION – Prevent CTE Infinite Loop T-SQL Paging Query Technique Comparison (OVER and ROW_NUMBER()) – CTE vs. Derived Table [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stone</title>
		<link>http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-240061</link>
		<dc:creator><![CDATA[stone]]></dc:creator>
		<pubDate>Wed, 18 Jan 2012 07:53:24 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-240061</guid>
		<description><![CDATA[Possible! but you must used subquery technique..

select Row_Number() OVER(Order by col1) as RowNumber, col1, col2
from
(
   select col1, col2 from table1
   union
   select col1, col2 from table2
) as tmpTable
where RowNumber between 1 and 10

How this works?

First, we retrieved all the records in one or more tables and put it into a temporary table
Second, select the records from the temporary table and perform Row_Number

AND Mark Smit, SEE MY POST ABOVE (stone  October 20, 2011 at 3:57 PM)
just change the query in the declare cursor keyset...that technique is far more efficient than cte when it comes to large amount of record...

NOTE: if one or both tables containing large amount of records, the query will be slower ..

If you need help and more info about it just leave a comment here.]]></description>
		<content:encoded><![CDATA[<p>Possible! but you must used subquery technique..</p>
<p>select Row_Number() OVER(Order by col1) as RowNumber, col1, col2<br />
from<br />
(<br />
   select col1, col2 from table1<br />
   union<br />
   select col1, col2 from table2<br />
) as tmpTable<br />
where RowNumber between 1 and 10</p>
<p>How this works?</p>
<p>First, we retrieved all the records in one or more tables and put it into a temporary table<br />
Second, select the records from the temporary table and perform Row_Number</p>
<p>AND Mark Smit, SEE MY POST ABOVE (stone  October 20, 2011 at 3:57 PM)<br />
just change the query in the declare cursor keyset&#8230;that technique is far more efficient than cte when it comes to large amount of record&#8230;</p>
<p>NOTE: if one or both tables containing large amount of records, the query will be slower ..</p>
<p>If you need help and more info about it just leave a comment here.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark Smit</title>
		<link>http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-239960</link>
		<dc:creator><![CDATA[Mark Smit]]></dc:creator>
		<pubDate>Wed, 18 Jan 2012 04:08:49 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-239960</guid>
		<description><![CDATA[Is there a way to include a UNION between more than one table in the cte, and perform ordering so that paging with the row_number will work 

Example - This works with one table.

With cteresult as
(Select col1,
            col2, 
            col3, 
           Row_Number() OVER(Order by col1) as RowNumber 
from table1)
select * from cteresult 
where RowNumber between 1 and 10

Hoe can I include a union with another table, but the ordering must be across both tables.]]></description>
		<content:encoded><![CDATA[<p>Is there a way to include a UNION between more than one table in the cte, and perform ordering so that paging with the row_number will work </p>
<p>Example &#8211; This works with one table.</p>
<p>With cteresult as<br />
(Select col1,<br />
            col2,<br />
            col3,<br />
           Row_Number() OVER(Order by col1) as RowNumber<br />
from table1)<br />
select * from cteresult<br />
where RowNumber between 1 and 10</p>
<p>Hoe can I include a union with another table, but the ordering must be across both tables.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Deepthi</title>
		<link>http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-185754</link>
		<dc:creator><![CDATA[Deepthi]]></dc:creator>
		<pubDate>Mon, 31 Oct 2011 05:51:00 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-185754</guid>
		<description><![CDATA[Just want to follow up..]]></description>
		<content:encoded><![CDATA[<p>Just want to follow up..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Deepthi</title>
		<link>http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-185752</link>
		<dc:creator><![CDATA[Deepthi]]></dc:creator>
		<pubDate>Mon, 31 Oct 2011 05:48:18 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-185752</guid>
		<description><![CDATA[Hi Pinal
Is there any example where cte is used to improve the performance of a query..generally in most cases using cte gives bad performance right..]]></description>
		<content:encoded><![CDATA[<p>Hi Pinal<br />
Is there any example where cte is used to improve the performance of a query..generally in most cases using cte gives bad performance right..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stone</title>
		<link>http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-183577</link>
		<dc:creator><![CDATA[stone]]></dc:creator>
		<pubDate>Wed, 26 Oct 2011 04:37:15 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-183577</guid>
		<description><![CDATA[Additional information...
1. The above temporary table (@Result) is temporary table resides in memory not in temp database
2. The Cursor (curO) object can have any select query as long as MS SQL can recognized.
3.  Cursor object does not read the entire table once executed, instead it will position the cursor to the first record and wait for the next fetch next
4.  The heart of this is the FETCH ABSOLUTE n...you can instruct dbms to go into desired absolute position.  :-) 
5.  FETCHing is slower than any select statement but for few records (such as 2000 rows) this should be reasonably/exceptionaly fast enough
6.  I have tested this with about a 100K records and it retrieves the desired records in about less than 2.5 sec for first run...and a fraction of a seconds in the succeding runs! (MS SQL 2008, Windows 7, Intel Core 2 duo @2.93Ghz, 2 GB memory)]]></description>
		<content:encoded><![CDATA[<p>Additional information&#8230;<br />
1. The above temporary table (@Result) is temporary table resides in memory not in temp database<br />
2. The Cursor (curO) object can have any select query as long as MS SQL can recognized.<br />
3.  Cursor object does not read the entire table once executed, instead it will position the cursor to the first record and wait for the next fetch next<br />
4.  The heart of this is the FETCH ABSOLUTE n&#8230;you can instruct dbms to go into desired absolute position.  :-)<br />
5.  FETCHing is slower than any select statement but for few records (such as 2000 rows) this should be reasonably/exceptionaly fast enough<br />
6.  I have tested this with about a 100K records and it retrieves the desired records in about less than 2.5 sec for first run&#8230;and a fraction of a seconds in the succeding runs! (MS SQL 2008, Windows 7, Intel Core 2 duo @2.93Ghz, 2 GB memory)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stone</title>
		<link>http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-181144</link>
		<dc:creator><![CDATA[stone]]></dc:creator>
		<pubDate>Thu, 20 Oct 2011 10:27:37 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-181144</guid>
		<description><![CDATA[Creating row number requires dbms to pull all the records..(imagine 1 million records)

I created a script to make the trick... (note it will take time for first run)

declare @Result table (
ACCOUNTNO varchar(20),
CONTACT varchar(40)) 

declare curO cursor KEYSET  for
select ACCOUNTNO, CONTACT 
from	CONTACT
order by RecID

open curO

declare @ACCOUNTNO varchar(20)
declare @CONTACT varchar(40)

declare @PageSize int
set @PageSize = 20 -- CHANGE THE DESIRED PAGE SIZE
declare @PageNo int
set @PageNo = 200-- CHANGE PAGE NUMBER TO GET
declare @RecLoc int
set @RecLoc = (@PageSize * (@PageNo-1)) + 1

declare @CurrRow int
set @CurrRow = 0

fetch absolute @RecLoc from curO into @ACCOUNTNO, @CONTACT
while @@FETCH_STATUS = 0 begin
	set @CurrRow = @CurrRow + 1
	insert into @Result (ACCOUNTNO, CONTACT)
	values				(@ACCOUNTNO, @CONTACT)
	if @CurrRow &gt;= @PageSize goto g
	fetch next from curO into @ACCOUNTNO, @CONTACT
end
g:
close curO
deallocate curO

select * from @Result


ENJOY!!!!!!!!
You may send email at [email removed] for questions :)]]></description>
		<content:encoded><![CDATA[<p>Creating row number requires dbms to pull all the records..(imagine 1 million records)</p>
<p>I created a script to make the trick&#8230; (note it will take time for first run)</p>
<p>declare @Result table (<br />
ACCOUNTNO varchar(20),<br />
CONTACT varchar(40)) </p>
<p>declare curO cursor KEYSET  for<br />
select ACCOUNTNO, CONTACT<br />
from	CONTACT<br />
order by RecID</p>
<p>open curO</p>
<p>declare @ACCOUNTNO varchar(20)<br />
declare @CONTACT varchar(40)</p>
<p>declare @PageSize int<br />
set @PageSize = 20 &#8212; CHANGE THE DESIRED PAGE SIZE<br />
declare @PageNo int<br />
set @PageNo = 200&#8211; CHANGE PAGE NUMBER TO GET<br />
declare @RecLoc int<br />
set @RecLoc = (@PageSize * (@PageNo-1)) + 1</p>
<p>declare @CurrRow int<br />
set @CurrRow = 0</p>
<p>fetch absolute @RecLoc from curO into @ACCOUNTNO, @CONTACT<br />
while @@FETCH_STATUS = 0 begin<br />
	set @CurrRow = @CurrRow + 1<br />
	insert into @Result (ACCOUNTNO, CONTACT)<br />
	values				(@ACCOUNTNO, @CONTACT)<br />
	if @CurrRow &gt;= @PageSize goto g<br />
	fetch next from curO into @ACCOUNTNO, @CONTACT<br />
end<br />
g:<br />
close curO<br />
deallocate curO</p>
<p>select * from @Result</p>
<p>ENJOY!!!!!!!!<br />
You may send email at [email removed] for questions :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: AbhIShek Online4all</title>
		<link>http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-156432</link>
		<dc:creator><![CDATA[AbhIShek Online4all]]></dc:creator>
		<pubDate>Fri, 12 Aug 2011 07:08:43 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-156432</guid>
		<description><![CDATA[create table #tbl (id int , c1 varchar(10), c2 varchar(10), c3 varchar(10))
insert into #tbl

select 1 , &#039;c&#039; , &#039;d&#039; , &#039;e&#039; union all
select 2 , &#039;d&#039; , &#039;e&#039; , &#039;f&#039; union all
select 3 , &#039;d&#039; , &#039;f&#039; , &#039;g&#039; 

select * from #tbl

declare @c1 varchar(100) , @c2 varchar(100) , @c3 varchar(100)
select @c1 = &#039;&#039; , @c2=&#039;&#039; , @c3=&#039;&#039;
select @c1 = @c1+c1 , @c2=@c2+c2 , @c3=@c3+c3 from #tbl
select @c1 c1 , @c2 c2 , @c3 c3 

drop table #tbl

Thanks,
abhIShek onlin4all]]></description>
		<content:encoded><![CDATA[<p>create table #tbl (id int , c1 varchar(10), c2 varchar(10), c3 varchar(10))<br />
insert into #tbl</p>
<p>select 1 , &#8216;c&#8217; , &#8216;d&#8217; , &#8216;e&#8217; union all<br />
select 2 , &#8216;d&#8217; , &#8216;e&#8217; , &#8216;f&#8217; union all<br />
select 3 , &#8216;d&#8217; , &#8216;f&#8217; , &#8216;g&#8217; </p>
<p>select * from #tbl</p>
<p>declare @c1 varchar(100) , @c2 varchar(100) , @c3 varchar(100)<br />
select @c1 = &#8221; , @c2=&#8221; , @c3=&#8221;<br />
select @c1 = @c1+c1 , @c2=@c2+c2 , @c3=@c3+c3 from #tbl<br />
select @c1 c1 , @c2 c2 , @c3 c3 </p>
<p>drop table #tbl</p>
<p>Thanks,<br />
abhIShek onlin4all</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: AbhIShek Online4all</title>
		<link>http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-156430</link>
		<dc:creator><![CDATA[AbhIShek Online4all]]></dc:creator>
		<pubDate>Fri, 12 Aug 2011 07:07:07 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-156430</guid>
		<description><![CDATA[create table #tbl (id int , c1 varchar(10), c2 varchar(10), c3 varchar(10))
insert into #tbl

select 1 , &#039;c&#039; , &#039;d&#039; , &#039;e&#039; union all
select 2 , &#039;d&#039; , &#039;e&#039; , &#039;f&#039; union all
select 3 , &#039;d&#039; , &#039;f&#039; , &#039;g&#039; 

select * from #tbl

declare @c1 varchar(100) , @c2 varchar(100) , @c3 varchar(100)
select @c1 = &#039;&#039; , @c2=&#039;&#039; , @c3=&#039;&#039;
select @c1 = @c1+c1 , @c2=@c2+c2 , @c3=@c3+c3 from #tbl
select @c1 c1 , @c2 c2 , @c3 c3 

drop table #tbl]]></description>
		<content:encoded><![CDATA[<p>create table #tbl (id int , c1 varchar(10), c2 varchar(10), c3 varchar(10))<br />
insert into #tbl</p>
<p>select 1 , &#8216;c&#8217; , &#8216;d&#8217; , &#8216;e&#8217; union all<br />
select 2 , &#8216;d&#8217; , &#8216;e&#8217; , &#8216;f&#8217; union all<br />
select 3 , &#8216;d&#8217; , &#8216;f&#8217; , &#8216;g&#8217; </p>
<p>select * from #tbl</p>
<p>declare @c1 varchar(100) , @c2 varchar(100) , @c3 varchar(100)<br />
select @c1 = &#8221; , @c2=&#8221; , @c3=&#8221;<br />
select @c1 = @c1+c1 , @c2=@c2+c2 , @c3=@c3+c3 from #tbl<br />
select @c1 c1 , @c2 c2 , @c3 c3 </p>
<p>drop table #tbl</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: AbhIShek Online4all</title>
		<link>http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-154840</link>
		<dc:creator><![CDATA[AbhIShek Online4all]]></dc:creator>
		<pubDate>Mon, 08 Aug 2011 04:52:13 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-154840</guid>
		<description><![CDATA[Hi Dave,
Is it possible to get the result, with out using loop?
Or suggest me any easiest way to achieve the result.

Thanks in advance,


create table #tbl (id int , c1 varchar(10), c2 varchar(10), c3 varchar(10))
insert into #tbl

select 1 , &#039;c&#039; , &#039;d&#039; , &#039;e&#039; union all
select 2 , &#039;d&#039; , &#039;e&#039; , &#039;f&#039; union all
select 3 , &#039;d&#039; , &#039;f&#039; , &#039;g&#039; 

select * from #tbl
drop table #tbl

--Result:
-------------------
--&#124; c1 &#124; c2  &#124;c3  &#124;
--&#124;----&#124;-----&#124;----&#124;   
--&#124; cdd&#124; def &#124;efg &#124;
-------------------]]></description>
		<content:encoded><![CDATA[<p>Hi Dave,<br />
Is it possible to get the result, with out using loop?<br />
Or suggest me any easiest way to achieve the result.</p>
<p>Thanks in advance,</p>
<p>create table #tbl (id int , c1 varchar(10), c2 varchar(10), c3 varchar(10))<br />
insert into #tbl</p>
<p>select 1 , &#8216;c&#8217; , &#8216;d&#8217; , &#8216;e&#8217; union all<br />
select 2 , &#8216;d&#8217; , &#8216;e&#8217; , &#8216;f&#8217; union all<br />
select 3 , &#8216;d&#8217; , &#8216;f&#8217; , &#8216;g&#8217; </p>
<p>select * from #tbl<br />
drop table #tbl</p>
<p>&#8211;Result:<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
&#8211;| c1 | c2  |c3  |<br />
&#8211;|&#8212;-|&#8212;&#8211;|&#8212;-|<br />
&#8211;| cdd| def |efg |<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQL SERVER – Common Table Expression (CTE) and Few Observation Journey to SQLAuthority</title>
		<link>http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-133697</link>
		<dc:creator><![CDATA[SQL SERVER – Common Table Expression (CTE) and Few Observation Journey to SQLAuthority]]></dc:creator>
		<pubDate>Tue, 10 May 2011 02:17:08 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-133697</guid>
		<description><![CDATA[[...] SQL SERVER – 2005 T-SQL Paging Query Technique Comparison (OVER and ROW_NUMBER()) – CTE vs. Deri... [...]]]></description>
		<content:encoded><![CDATA[<p>[...] SQL SERVER – 2005 T-SQL Paging Query Technique Comparison (OVER and ROW_NUMBER()) – CTE vs. Deri&#8230; [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: shyam</title>
		<link>http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-124426</link>
		<dc:creator><![CDATA[shyam]]></dc:creator>
		<pubDate>Tue, 22 Mar 2011 20:14:21 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-124426</guid>
		<description><![CDATA[Thanks however what about the tables need to be join?
Shyam]]></description>
		<content:encoded><![CDATA[<p>Thanks however what about the tables need to be join?<br />
Shyam</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: chirag</title>
		<link>http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-112221</link>
		<dc:creator><![CDATA[chirag]]></dc:creator>
		<pubDate>Thu, 20 Jan 2011 13:35:03 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-112221</guid>
		<description><![CDATA[Hey guys based the above query i have come up with a genric idea of fetching data from any table..

check this out . 
please ppl do let me know if there is any problem or it works for you like with me ... enjoy..

Create PROCEDURE [dbo].[GetData] 
(
@Pagesize int, 
@Pagenumber int,
@Tablename nVarchar(50),
@ColumnList nVarchar(500),
@Orderby nVarchar(50)
)
AS
begin
Declare @FirstRow nVarchar(20)
Declare @LastRow nVarchar(20)
Declare @query nVarchar (2000)

SET @Pagenumber=@Pagenumber-1
Select @FirstRow = @Pagenumber * @Pagesize + 1,
@LastRow = @PageNumber * @PageSize + @PageSize ;

set @query =&#039;WITH cte_Table(PageNumber,&#039; +@ColumnList +&#039; )   AS   (   SELECT  ROW_NUMBER() OVER    (ORDER BY &#039;+@Orderby+&#039; ASC )  AS PageNumber,&#039;+@ColumnList+&#039; FROM &#039;+ @Tablename+&#039;  ) SELECT * FROM cte_Table WHERE PageNumber  Between &#039;+@FirstRow +&#039; and &#039;+ @LastRow
print @query 
EXEC sp_executesql @query 
END]]></description>
		<content:encoded><![CDATA[<p>Hey guys based the above query i have come up with a genric idea of fetching data from any table..</p>
<p>check this out .<br />
please ppl do let me know if there is any problem or it works for you like with me &#8230; enjoy..</p>
<p>Create PROCEDURE [dbo].[GetData]<br />
(<br />
@Pagesize int,<br />
@Pagenumber int,<br />
@Tablename nVarchar(50),<br />
@ColumnList nVarchar(500),<br />
@Orderby nVarchar(50)<br />
)<br />
AS<br />
begin<br />
Declare @FirstRow nVarchar(20)<br />
Declare @LastRow nVarchar(20)<br />
Declare @query nVarchar (2000)</p>
<p>SET @Pagenumber=@Pagenumber-1<br />
Select @FirstRow = @Pagenumber * @Pagesize + 1,<br />
@LastRow = @PageNumber * @PageSize + @PageSize ;</p>
<p>set @query =&#8217;WITH cte_Table(PageNumber,&#8217; +@ColumnList +&#8217; )   AS   (   SELECT  ROW_NUMBER() OVER    (ORDER BY &#8216;+@Orderby+&#8217; ASC )  AS PageNumber,&#8217;+@ColumnList+&#8217; FROM &#8216;+ @Tablename+&#8217;  ) SELECT * FROM cte_Table WHERE PageNumber  Between &#8216;+@FirstRow +&#8217; and &#8216;+ @LastRow<br />
print @query<br />
EXEC sp_executesql @query<br />
END</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQL SERVER – Server Side Paging in SQL Server 2011 – A Better Alternative Journey to SQL Authority with Pinal Dave</title>
		<link>http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-105623</link>
		<dc:creator><![CDATA[SQL SERVER – Server Side Paging in SQL Server 2011 – A Better Alternative Journey to SQL Authority with Pinal Dave]]></dc:creator>
		<pubDate>Wed, 15 Dec 2010 01:31:35 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-105623</guid>
		<description><![CDATA[[...] is the blog article where I wrote about SQL Server 2005/2008 paging method  SQL SERVER – 2005 T-SQL Paging Query Technique Comparison (OVER and ROW_NUMBER()) – CTE vs. Deri.... One can achieve this using OVER clause and ROW_NUMBER() [...]]]></description>
		<content:encoded><![CDATA[<p>[...] is the blog article where I wrote about SQL Server 2005/2008 paging method  SQL SERVER – 2005 T-SQL Paging Query Technique Comparison (OVER and ROW_NUMBER()) – CTE vs. Deri&#8230;. One can achieve this using OVER clause and ROW_NUMBER() [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael J. Ryan</title>
		<link>http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-70661</link>
		<dc:creator><![CDATA[Michael J. Ryan]]></dc:creator>
		<pubDate>Thu, 13 May 2010 04:19:50 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-70661</guid>
		<description><![CDATA[Just wanted to point out a recent post I made on this topic, it discusses how this works, as well as a broader example for extracting paged result sets...

http://frugalcoder.us/post/2010/02/23/tsql-paged-result-sproc.aspx]]></description>
		<content:encoded><![CDATA[<p>Just wanted to point out a recent post I made on this topic, it discusses how this works, as well as a broader example for extracting paged result sets&#8230;</p>
<p><a href="http://frugalcoder.us/post/2010/02/23/tsql-paged-result-sproc.aspx" rel="nofollow">http://frugalcoder.us/post/2010/02/23/tsql-paged-result-sproc.aspx</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: FARHAD</title>
		<link>http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-68832</link>
		<dc:creator><![CDATA[FARHAD]]></dc:creator>
		<pubDate>Tue, 04 May 2010 10:00:38 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-68832</guid>
		<description><![CDATA[tnx , i do it by your Sample]]></description>
		<content:encoded><![CDATA[<p>tnx , i do it by your Sample</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pinal Dave</title>
		<link>http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-59391</link>
		<dc:creator><![CDATA[Pinal Dave]]></dc:creator>
		<pubDate>Wed, 06 Jan 2010 07:02:29 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-59391</guid>
		<description><![CDATA[Hi Mark,

The condition &quot;AND row_number=21&quot; in WHERE clause is not clear. Can you provide the full statement?

Regards,
Pinal Dave]]></description>
		<content:encoded><![CDATA[<p>Hi Mark,</p>
<p>The condition &#8220;AND row_number=21&#8243; in WHERE clause is not clear. Can you provide the full statement?</p>
<p>Regards,<br />
Pinal Dave</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark Stuart</title>
		<link>http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-59377</link>
		<dc:creator><![CDATA[Mark Stuart]]></dc:creator>
		<pubDate>Wed, 06 Jan 2010 00:14:44 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-59377</guid>
		<description><![CDATA[Hi Pinal,
I&#039;m using the ROW_NUMBER() and OVER in my DSQL statement for a &quot;paging&quot; technique. I&#039;m doing this because of large data sets. 

In the SELECT WHERE, I use: row_number&gt;=1 AND row_number=21 AND row_number&lt;=40, there maybe no row numbers in that range, but records do exist to be displayed.

1st record set:
1
2
4
5
7
8
13
15
19
(9) rows

2nd record set may only return 5 rows but there are many more:
21
22
24
25
28

ROW_NUMBER doesn&#039;t work as I&#039;d anticipated, where I want to get the next 20 records and append them to the table display object.

Hopefully, that makes sense.
Mark]]></description>
		<content:encoded><![CDATA[<p>Hi Pinal,<br />
I&#8217;m using the ROW_NUMBER() and OVER in my DSQL statement for a &#8220;paging&#8221; technique. I&#8217;m doing this because of large data sets. </p>
<p>In the SELECT WHERE, I use: row_number&gt;=1 AND row_number=21 AND row_number&lt;=40, there maybe no row numbers in that range, but records do exist to be displayed.</p>
<p>1st record set:<br />
1<br />
2<br />
4<br />
5<br />
7<br />
8<br />
13<br />
15<br />
19<br />
(9) rows</p>
<p>2nd record set may only return 5 rows but there are many more:<br />
21<br />
22<br />
24<br />
25<br />
28</p>
<p>ROW_NUMBER doesn&#039;t work as I&#039;d anticipated, where I want to get the next 20 records and append them to the table display object.</p>
<p>Hopefully, that makes sense.<br />
Mark</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nandish</title>
		<link>http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-57079</link>
		<dc:creator><![CDATA[Nandish]]></dc:creator>
		<pubDate>Tue, 27 Oct 2009 14:18:31 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-57079</guid>
		<description><![CDATA[Hi Pinal,

Great query...

Fantastic..it works perfactly...]]></description>
		<content:encoded><![CDATA[<p>Hi Pinal,</p>
<p>Great query&#8230;</p>
<p>Fantastic..it works perfactly&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Faisal</title>
		<link>http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-56847</link>
		<dc:creator><![CDATA[Faisal]]></dc:creator>
		<pubDate>Wed, 21 Oct 2009 10:14:56 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-56847</guid>
		<description><![CDATA[Great Query.. 

I think u&#039;r master of Query.. hahaha

Thank u  very much... :)]]></description>
		<content:encoded><![CDATA[<p>Great Query.. </p>
<p>I think u&#8217;r master of Query.. hahaha</p>
<p>Thank u  very much&#8230; :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQL SERVER - Guidelines and Coding Standards Part - 1 Journey to SQL Authority with Pinal Dave</title>
		<link>http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-47496</link>
		<dc:creator><![CDATA[SQL SERVER - Guidelines and Coding Standards Part - 1 Journey to SQL Authority with Pinal Dave]]></dc:creator>
		<pubDate>Thu, 26 Feb 2009 12:13:43 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/06/11/sql-server-2005-t-sql-paging-query-technique-comparison-over-and-row_number-cte-vs-derived-table/#comment-47496</guid>
		<description><![CDATA[[...] Avoid using temporary tables and derived tables as it uses more disks I/O. Instead use CTE (Common Table Expression); its scope is limited to the next statement in SQL query. (Read More Here) [...]]]></description>
		<content:encoded><![CDATA[<p>[...] Avoid using temporary tables and derived tables as it uses more disks I/O. Instead use CTE (Common Table Expression); its scope is limited to the next statement in SQL query. (Read More Here) [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>
