Re: How To Return A "Range Of Rows"??



pbd22 (dushkin@xxxxxxxxx) writes:
I "am" using 2005 and the solution looks much easier than the 2000
alternatives.
I am not sure why, but the second message I posted in this thread was
right after the first but it appeared after your response. Regardless,
as you can see my queries are somewhat complicated and I am not quite
sure how to apply your 2005 solution (OrdersCTE) to them. I am
wondering if you could use one of my queries to demonstrate how it
works (I hope I am not being too demanding)?
It would help me get my mind wrapped around things.

WITH CTE (
SELECT <yourselectlistgoeshere>,
rownum = row_number() over( ORDER BY <yourorderbylisthere>)
FROM <yourfrom+joinshere>
WHERE <anywhereclauseyoumayhav>
)
SELECT <yourselectlisthereagain, nowwithoutaliases>
FROM CTE
WHERE rownum BETWEEN @first AND @last
ORDER BY rownum


--
Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
.



Relevant Pages

  • Re: Getting @@RowCount when using ROW_NUMBER()
    ... The methods suggested would result in running the query twice. ... ROW_NUMBEROVER AS rownum ... Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx ... Books Online for SQL Server 2005 at ...
    (microsoft.public.sqlserver.programming)
  • Re: Explicit IN clause vs JOIN. Plan tanks with Join, Why?
    ... WIthout the CTE, the plan is horrible (scans on nearly ... relationally significant. ... Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books ... ...
    (comp.databases.ms-sqlserver)
  • Re: Supress Repeating Name
    ... unless you define an alias for the CTE. ... Also, the TOP and the ORDER BY clauses should be in the final query, not the ... Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx ... Books Online for SQL Server 2005 at ...
    (microsoft.public.sqlserver.programming)
  • Re: Creating a dynamic Global Temp Table within a stored procedure
    ... there is indeed less need for temp tables these days. ... WITH CTE AS ... While this syntax is neat and pure, the full story is that SQL Server ... Books Online for SQL Server 2005 at ...
    (comp.databases.ms-sqlserver)
  • Re: Remove neighbouring duplicates
    ... Since you did not say which version of SQL Server you are using, ... WITH numbered_items (rownum, symbol, position, qty, price, date) ... Books Online for SQL Server 2005 at ...
    (comp.databases.ms-sqlserver)