Groups | Blog | Home
all groups > sql server programming > october 2005 >

sql server programming : Yet another SQL paging solution


Ken
10/19/2005 10:46:01 PM
I know this is a well-trodden road, but I've come up with a SQL paging
solution that I can't seem to match up with the many approaches I've
seen online. Feedback on "my" approach would be appreciated.

The following statement to get the TOP x rows, and we add a criteria
which will exclude the rows we've already retrieved. The goal is to
get the first x rows starting at row r. My current query is:

SELECT TOP x *
FROM Table
WHERE IDColumn NOT IN ( SELECT TOP r IDColumn
FROM Table
ORDER BY SortColumn)
ORDER BY SortColumn;
Ken
10/19/2005 11:30:56 PM
Uri - Thanks for the links. I'd checked them out already and didn't
see one that resembled my solution... but my solution appears to work.
Does it have any fleas?

David - I understand that I'm not able to parameterize the TOP x value,
but I've already crossed the bridge of using dynamic SQL for some
architectural reasons. Other than the fact that my SQL has to be
dynamic, are there any problems with it?
David Portas
10/20/2005 2:35:38 AM
An example of this method is given in:
http://www.aspfaq.com/show.asp?id=2120
(the section headed "Dynamic SQL").

Another potential catch is that TOP (without the TIES option) isn't
deterministic unless you specify a unique key in the ORDER BY list.
That means you could miss or duplicate rows between pages (although the
same potential problem is common to other paging methods). Make sure
you specify a unique sort order.

--
David Portas
SQL Server MVP
--
David Portas
10/20/2005 7:09:18 AM
The problem is that in SQL2000 you can't parameterize the TOP x value. You'd
have to use dynamic SQL to do that. In SQL2005 it is possible using static
SQL.

--
David Portas
SQL Server MVP
--

[quoted text, click to view]

Uri Dimant
10/20/2005 7:59:09 AM
Ken
See, if this helps
http://www.aspfaq.com/show.asp?id=2120
http://www.aspfaq.com/show.asp?id=2424





[quoted text, click to view]

xslspy
10/20/2005 8:25:05 AM
May be the solution works , but the fact is most of the times we may not be
having the IDcolumn , Doing Paging on the table which doesnt have the ID
column is the issue.can any one have better solutions for this?

[quoted text, click to view]
David Portas
10/20/2005 8:36:47 AM
You don't need an "ID" column. You just need a key (either single
column or a compound key). Hopefully all your tables have keys! :-)

--
David Portas
SQL Server MVP
--
xslspy
10/20/2005 9:06:04 AM
Is IDColumn something built in that comes with all tables? when you look at
the query ...
IDColumn NOT IN ( SELECT TOP r IDColumn
FROM Table
ORDER BY SortColumn)

IDColumn should be numbered column rite? please clarify


[quoted text, click to view]
David Portas
10/20/2005 9:46:03 AM
No "Idcolumn" is just Ken's example. ANY column or columns will do for
the sort order but they should be unique. That shouldn't be a problem
because every table should have a key. In SQL it's fundamentally
important that every table have at least one key but it's up to the
database designer to create it by declaring it with a UNIQUE or PRIMARY
KEY constraint.

--
David Portas
SQL Server MVP
--
xslspy
10/20/2005 2:19:02 PM
Oh , its cool. but when i try on temp tables with out any key it seems
working,will it work on tables with out key also?

[quoted text, click to view]
David Portas
10/24/2005 2:44:26 AM
Not necessarily, because there's no easy way to guarantee that
duplicate rows won't be skipped or duplicated across pages. But why
would you want to use a table without a key?

--
David Portas
SQL Server MVP
--
vsr
11/27/2005 5:26:01 PM
Thank you for the clarification , i am using this logic in my application and
its working like champ

[quoted text, click to view]
AddThis Social Bookmark Button