Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 get row x to y - how?

Author  Topic 

fabianus76
Posting Yak Master

191 Posts

Posted - 2006-01-15 : 10:47:45
I know the "top" function to get the first x rows.
What I am looking for is how to get row x to y, not starting from the top (for example form 1000 to 2000).
Thank you for any advice!

Regards,
Fabian

my favorit hoster is ASPnix : www.aspnix.com !

Srinika
Master Smack Fu Yak Hacker

1378 Posts

Posted - 2006-01-15 : 11:04:01
Try the following

select top 1000 *
from (select top 2000 * from Table1 order by id) t
order by id desc

or if u want from any value to any other, have a function to pass ur start & end, create the query dynamically, execute to get the result
Go to Top of Page

fabianus76
Posting Yak Master

191 Posts

Posted - 2006-01-15 : 11:20:43
Hi Srinika,

thank you for this. Unfortunatly I do not have an ID in this table.
In MySQL you might select row x to y, that's why I though about it.

you write :
"or if u want from any value to any other, have a function to pass ur start & end, create the query dynamically, execute to get the result"

I do not understand - how could I "have a function" ?

Regards,
Fabian

my favorit hoster is ASPnix : www.aspnix.com !
Go to Top of Page

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2006-01-15 : 11:27:44
Without an ID value or some other method of consistently ordering the data, you cannot guarantee getting consistent results even with the TOP clause, let along grabbing a set of records from the middle of the dataset.
Go to Top of Page

Srinika
Master Smack Fu Yak Hacker

1378 Posts

Posted - 2006-01-15 : 12:24:20
quote:
Originally posted by fabianus76

... I do not have an ID in this table.....


To get range of records meaningfully (blindman also saying the same) u should have some field in an "order by".
U may have some primary key or any field from which u want this records to be sorted when selecting. Use that field. It doesn't have to be the field called "ID"

quote:
Originally posted by fabianus76

... you write :
"or if u want from any value to any other, have a function to pass ur start & end, create the query dynamically, execute to get the result"

I do not understand - how could I "have a function" ?



my any value is, ur x or 1000 in ur example
my any other is, ur y or 2000 in ur example

Function is something that u have to write (UDF)
Functions can have parameters, Create the function so as to accept 2 parameters.
Get those parameters and write a dynamic SQL query
Execute it.

U need to Refer some material, may be Books OnLine and learn it. I can tell u, but u won't learn it, unless doing it urself.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-01-16 : 00:01:29
If you meant pagination, refer these
http://weblogs.sqlteam.com/jeffs/archive/2003/12/22/672.aspx
http://www.aspfaq.com/show.asp?id=2120

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -