Solved

Get First N Records

Posted on 2000-04-13
6
182 Views
Last Modified: 2010-05-02
This isn't really strictly a VB question, but I'll try it anyway.

ENVIRONMENT:  VB6 SP-3, ADO 2.1, Oracle 8i.

I need an Oracle-friendly SQL statement that can return the first n records from a given table, using a sort criteria specified by me--or using no sort criteria at all, and relying on the native row ordering.

Selecting the entire rowset, then using GetRows() or something like that isn't really useful.  I could have gajillions of records in this table, and I just need the first 28--and exactly 28.

Is there any way to do this?  Closest thing I know of is "SELECT TOP N PERCENT", but that's a Jet-thing only, besides not fitting the bill anyway.

Seems like I would have heard of something by now if there were a way to do this.  I think it's a no-go.

Thanks in advance for any helpful help.

0
Comment
Question by:bryker
6 Comments
 
LVL 8

Expert Comment

by:stefanx
ID: 2714181
Bryker, I'd very much like to know this too. Although I don't use Oracle, exactly the same issue exists with SQL Server. Pity that the "SELECT TOP x" thing is Jet specific. You could of course create an MS-Access DB and link the Oracle tables to it through ODBC or OLEDB. Using this would allow you to use the "SELECT TOP x" statement on the Access DB although I would guess it to be a major performance killer.

Perhaps using the trick of sorting the records DESC according to your requirements and using ADO paging might do this, but I'm not that familiar with it.

Anyone else ?
0
 

Accepted Solution

by:
NickMP earned 100 total points
ID: 2714203

The Oracle function for this is ROWNUM do something like

SELECT * FROM CLIENTS WHERE ROWNUM <= 15

will return you the first 15 rows.
0
 
LVL 6

Expert Comment

by:Marine
ID: 2714216
To Stephan. If you using SQL or Access then your solution is correct. When retrievent x recrods you have to order by asc or desc depending if you want to retrieve them from top to buttom or last to top.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:bryker
ID: 2714244
Great!  Thanks a lot.  I was wondering why I'd never heard of this--until I tried it in Access...splat!  Can't believe that it's never found its way into Jet.  But as stefanx said, there's a way to do that with SELECT TOP anyway...sort of.

Thanks again.

0
 
LVL 8

Expert Comment

by:stefanx
ID: 2714247
I quickly checked this out for SQL Server if anyone is interested, it is :

SET ROWCOUNT=28
SELECT * FROM CLIENTS

0
 
LVL 8

Expert Comment

by:stefanx
ID: 2714248
I quickly checked this out for SQL Server if anyone is interested, it is :

SET ROWCOUNT 28
SELECT * FROM CLIENTS

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

911 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

21 Experts available now in Live!

Get 1:1 Help Now