Wednesday, March 7, 2012

Pagination

Hi,
I have recently moved into ms sql from mysql and having problems with finding a query to help paginate my search results. In mysql I would use:
SELECT * FROM tablename WHERE something = something LIMIT 0,10
what would a similar query be in sql server?
From what I have experienced so far I know this is going to be a big query!
Thanks in advance
Steve
209 views and no replies!!
I went out and managed to find a solution on my own.
cheers for all the help
|||

can you paste the solution. i will help me too..

cheers

|||Yeah sure,
SELECT TOP $records_per_page * FROM tbl_name WHERE field LIKE '$getID'
AND id NOT IN (SELECT TOP $page id FROM tbl_name)
This is the dymanic version. In a nutshell;
SELECT TOP $records_per_page* FROM answers - selects the first 'n' records from table
AND id NOT IN (SELECT TOP $page id FROM answers) - id has to be a unique identifier, NOT IN is declaring the offset; where id is not in the top $page of this table
so
SELECT TOP 10 * FROM tbl_name WHERE field LIKE 'steve'
AND id NOT IN (SELECT TOP 20 id FROM tbl_name)
will return records 11-20 (presuming there are that many records)
alternatives can be found here

No comments:

Post a Comment