How to limit ADO recordsets

I need to be able to limit the number of records returned from a query against an Oracle database to 50 at a time without retieving all of the records to the client at once. After the user has viewed the first 50 records they should then be able to click a button that retrieves the next 50 records and so on. How would I do this?
dk02151Asked:
Who is Participating?
 
PNJConnect With a Mentor Commented:
What about: "SELECT stuff FROM table WHERE selection criteria and ROWNUM <= 50 ORDER BY primary key

then when you hit the last record you can add " AND primary key >= <last value> AND ROWNUM <= 50".

ROWNUM is a inbuilt variable that Oracle defines that limits the number of records returned. It's up to you how to select the correct one(s) in the first place.
0
 
deightonprogCommented:
how are you getting the reords?  Are these data bound controls?  or are you processing recordsets via code.
0
 
dk02151Author Commented:
We are getting the records through code only.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
gencrossCommented:
Using ADO you can set the Maxrecords property to 50 before opening the recordset.

rstADO.MaxRecords = 50

I'll get back to you on getting the next 50 records.
0
 
gencrossCommented:
Ok.  What makes the most sense is retrieving all of the records each time.  And each time only show 50.  The first time show records 1-50.  The next time show records 51-100.  All you need to do is loop through the recordset until you get to the record you want to begin, and then show the next 50.

This way if you ever want to display 20 or 60 at a time all you will need to change is one variable.

It may sound like this could be slow, but after your initial search the records should be cached on the server and the next searches should be fast.

I believe this is the way quite a few internet search sites work.

If you need an example let me know.
0
 
gencrossCommented:
PNJ, I thought something like that might work, but It seems it could cause problems if you want to change the order by at sometime.  I don't think it will work if you want to order the records by something else at sometime, perhaps for easier viewing.

Please let me know if I am wrong:)
0
 
flytigerCommented:
'dim global variable current_index and max_length
max_length = 50

'in first recordset
current_index = 1

'when you click next
current_index = current_index + max_length

'sql statement here
sqlstmt="SELECT stuff FROM table WHERE selection criteria and ROWNUM >= " & current_index & " ROWNUM < " & (current_index + max_length)

0
 
dk02151Author Commented:
The selection criteria looks something like this:
"select * from TableName where PolicyNumber = 'XXXXXXX'"
This can retrieve several thousand cecords that are not consecutive with respect to the primary key which is an Oracle sequence number. Is there anything within the Oracle database itself that will do this? I am currently getting all of the records and displaying 50 at a time but getting all of the records can take too long when there is several thousand returned records.
0
 
dk02151Author Commented:
I see what you are saying now. I was not aware that ROWNUM was a built in Oracle variable. This looks like it might work. I will try it.
0
 
dk02151Author Commented:
I see what you are saying now. I was not aware that ROWNUM was a built in Oracle variable. This looks like it might work. I will try it.
0
 
dk02151Author Commented:
ROWNUM will not work. If I try doing ="SELECT stuff FROM table WHERE selection criteria and ROWNUM >= " & current_index & " ROWNUM < " & (current_index + max_length) I get no rows selected. ROWNUM only worked if the starting point is ROWNUM=1. In that case I am better of returning everything at once. Any other ideas?
0
 
dk02151Author Commented:
ROWNUM will not work. If I try doing ="SELECT stuff FROM table WHERE selection criteria and ROWNUM >= " & current_index & " ROWNUM < " & (current_index + max_length) I get no rows selected. ROWNUM only worked if the starting point is ROWNUM=1. In that case I am better of returning everything at once. Any other ideas?
0
 
dk02151Author Commented:
I got ROWNUM to work using the suggestion " AND primary key >= <last value> AND ROWNUM <= 50"
0
 
flytigerCommented:
'Change your sql statement as following

sqlstmt="SELECT stuff FROM (SELECT stuff, rownum rn FROM table WHERE selection criteria) where rn >= " & current_index & " AND rn < " & (current_index + max_length)

'note: if you use ORDER BY. The result won't be correct anymore since ROWNUM was generated before ORDER BY
0
 
flytigerCommented:
'if you want to use ORDER BY

sqlstmt="SELECT stuff FROM (SELECT stuff, rownum rn FROM (SELECT stuff FROM table WHERE selection criteria ORDER BY something)) WHERE rn >= " & current_index & " AND rn < " & (current_index + max_length)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.