Solved

How to limit ADO recordsets

Posted on 2002-06-26
15
157 Views
Last Modified: 2012-05-05
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?
0
Comment
Question by:dk02151
  • 7
  • 3
  • 3
  • +2
15 Comments
 
LVL 18

Expert Comment

by:deighton
Comment Utility
how are you getting the reords?  Are these data bound controls?  or are you processing recordsets via code.
0
 

Author Comment

by:dk02151
Comment Utility
We are getting the records through code only.
0
 
LVL 4

Expert Comment

by:gencross
Comment Utility
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
 
LVL 4

Expert Comment

by:gencross
Comment Utility
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
 
LVL 3

Accepted Solution

by:
PNJ earned 200 total points
Comment Utility
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
 
LVL 4

Expert Comment

by:gencross
Comment Utility
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
 
LVL 2

Expert Comment

by:flytiger
Comment Utility
'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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:dk02151
Comment Utility
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
 

Author Comment

by:dk02151
Comment Utility
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
 

Author Comment

by:dk02151
Comment Utility
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
 

Author Comment

by:dk02151
Comment Utility
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
 

Author Comment

by:dk02151
Comment Utility
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
 

Author Comment

by:dk02151
Comment Utility
I got ROWNUM to work using the suggestion " AND primary key >= <last value> AND ROWNUM <= 50"
0
 
LVL 2

Expert Comment

by:flytiger
Comment Utility
'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
 
LVL 2

Expert Comment

by:flytiger
Comment Utility
'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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now