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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 select a subset of records

Author  Topic 

aggiekevin
Starting Member

14 Posts

Posted - 2007-08-14 : 18:09:24
I've got a database which contains more than records. Returning all the records takes too long, so I'd like to be able to query a subset of 100 records at a time (records 0-99, 100-199, etc). How would I do this?

Thanks!

rmiao
Master Smack Fu Yak Hacker

7266 Posts

Posted - 2007-08-14 : 22:02:06
Does the table have any id column?
Go to Top of Page

aggiekevin
Starting Member

14 Posts

Posted - 2007-08-14 : 23:53:23
quote:
Originally posted by rmiao

Does the table have any id column?



Yes the table has an id column.
Go to Top of Page

rmiao
Master Smack Fu Yak Hacker

7266 Posts

Posted - 2007-08-15 : 00:08:39
Then you can select rows based on id.
Go to Top of Page

aggiekevin
Starting Member

14 Posts

Posted - 2007-08-15 : 10:29:49
quote:
Originally posted by rmiao

Then you can select rows based on id.



Yes but then I would always get a different number of records returned from the query (because sometimes a record is deleted).
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-08-15 : 10:32:57
[code]SELECT *
FROM
(
SELECT *,
row = row_number() OVER (ORDER BY id)
FROM yourtable
) a
WHERE row BETWEEN 100 AND 199[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

aggiekevin
Starting Member

14 Posts

Posted - 2007-08-15 : 10:40:43
Actually I was thinking and your idea seems like a good one. If I keep track of the id of the last record retrieved and pass it into the stored proc I could do something like:

SELECT TOP 100 [columnname]
FROM [table] t
WHERE t.id > @last_record_id

I did some testing and this accomplishes exactly what I wanted. Thanks!
Go to Top of Page

aggiekevin
Starting Member

14 Posts

Posted - 2007-08-15 : 10:46:14
quote:
Originally posted by khtan

SELECT	*
FROM
(
SELECT *,
row = row_number() OVER (ORDER BY id)
FROM yourtable
) a
WHERE row BETWEEN 100 AND 199



KH
[spoiler]Time is always against us[/spoiler]





Yeah that would be a nice solution, however I am using SQL Server 2000, so I cannot use the row_number() function. I'm sorry I didn't specify this in my original post. Thanks for the response though.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-08-15 : 10:48:33
You should have posted over in SQL 2000 forum not 2005.


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

aggiekevin
Starting Member

14 Posts

Posted - 2007-08-15 : 10:54:43
Yeah I apologize. I'm new to these forums and didn't realize there were different forums for the 2000 and 2005 versions. Thanks for your response though!
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-08-15 : 11:00:04
you can also take a look here
http://weblogs.sqlteam.com/jeffs/archive/2004/03/22/1085.aspx
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50012
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=22502


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-08-16 : 02:16:58
Also refer
http://www.aspfaq.com/show.asp?id=2120

Madhivanan

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

- Advertisement -