Re: sql select top clause

Author: Ted Roche

Posted: 2011-02-21 at 13:02:33

You can create a cursor on the server and move back and forth on it,

but that can be pretty complex and tie up the connection, expensive

both on client and server. I've used Progressive Fetch before, but

it's likely not what you want. However, try this:

A hack I ran into in a PHP framework (CodeIgniter) used this

workaround to emulate the "LIMIT" clause that other db have:

Take your SELECT <fieldlist> FROM <tables and joins> WHERE <clause>

ORDER BY <expression ASC/DESC>

Drop the 'top 20' and change to:

SELECT (row_number() OVER (order by expression), <fieldlist> FROM

<tables and joins> WHERE <clause> ORDER BY <expression ASC/DESC>) AS A

WHERE A.rownum BETWEEN (offset+1) AND (offset + limit)

Where offset is the starting row number (typically zero-based, hence

the plus one) and limit is the number of rows you want retrieved, 20

in your case.

This works with newer SQL Servers (I'm running it on 2008R2) but not

on older ones (I've heard it doesn't work on 2000, but you shouldn't

be using that anyway, right?)

On Mon, Feb 21, 2011 at 12:34 PM, Rafael Copquin <rcopquin@ciudad.com.ar> wrote:

> That's another way of doing it, thanks, but I'd rather have SQL give me

> the right answer.

>

>

> El 21/02/2011 12:16, Alan Bourke escribió:

>> Since they're all numeric, add a column in SQL Server of type integer,

>> copy the contents of itemcode into it, and use that instead.

>

--

Ted Roche

Ted Roche & Associates, LLC

http://www.tedroche.com

_______________________________________________

Post Messages to: ProFox@leafe.com

Subscription Maintenance: http://leafe.com/mailman/listinfo/profox

OT-free version of this list: http://leafe.com/mailman/listinfo/profoxtech

Searchable Archive: http://leafe.com/archives/search/profox

This message: http://leafe.com/archives/byMID/profox/AANLkTim0EUqGsvBj=Dit13t3b6nmEuBXc8csQZoEMSNf@mail.gmail.com

** All postings, unless explicitly stated otherwise, are the opinions of the author, and do not constitute legal or medical advice. This statement is added to the messages for those lawyers who are too stupid to see the obvious.

©2011 Ted Roche