File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Getting random records Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Getting random records" Watch "Getting random records" New topic
Author

Getting random records

Kudret Serin
Ranch Hand

Joined: Aug 02, 2005
Posts: 167

Hi all,
How can i get randomly 20 of 600 records from MSSSQL with SELECT statement?
Is there an SQL way?
Thanks in advance
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31069
    
232

Kudret,
I haven't used MSSQL, but there are two approaches I can think of. You'll need to check whether they are supported.

1) If the database supports random number generation, do an "order by" on that function and then use ROWNUM to return the first 20 rows.
2) Generate 20 random #s in Java between 1 and 600. Then add a clause to the WHERE to only return ROWNUMs matching those.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Kudret Serin
Ranch Hand

Joined: Aug 02, 2005
Posts: 167

Thanks for the reply




[ April 06, 2007: Message edited by: Kudret Serin ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting random records