GeeCON Prague 2014*
The moose likes JDBC and the fly likes  How to define ResultSet limit by PL/SQl in Oracle database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark " How to define ResultSet limit by PL/SQl in Oracle database" Watch " How to define ResultSet limit by PL/SQl in Oracle database" New topic
Author

How to define ResultSet limit by PL/SQl in Oracle database

somenath chatterjee
Greenhorn

Joined: Mar 20, 2008
Posts: 16
Hi all,
I am sharing my problem to all of you for need help to resolve it.
I am going to retrieve millions data from a table.Now I want to retrieve data
part by part wise by ResultSet, giving some example like,a table having 1000 of rows now I want to retrieve those data in 10 steps. like in one steps ResultSet limit 0 to 100, and after finish 1 step 2nd ResultSet limit come 101 to 200.
So if you have done like this or faced like it. please reply me.
karthikeyan Chockalingam
Ranch Hand

Joined: Sep 06, 2003
Posts: 259
The SQL to be used is database specific. Can you please mention the database [like MySQL or Oracle] you are using?


http://www.skillassert.com


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Karthi, the title of the post includes the phrase "in Oracle database". True, it would have been better to mention that in the body of the post, but at least it's somewhere.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30580
    
154

Somenath,
Oracle provides a keyword called "rownum" that allows you to return each piece. This Ask Tom article gives a sample.


[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
karthikeyan Chockalingam
Ranch Hand

Joined: Sep 06, 2003
Posts: 259
Paul, It was my fault not to notice it in the title. I was concentrating only on the content.
[ June 09, 2008: Message edited by: karthi keyan ]
somenath chatterjee
Greenhorn

Joined: Mar 20, 2008
Posts: 16
Hi,

Thanks for reply me. I have done with by rownum, its working. I am sending you the query please check it and let me know if any issues.

select * from ( select *, ROWNUM as rnum from table where ROWNUM <= "upperLimit" order by 1 ) where rnum > "lowerLimit"
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30580
    
154


While this might work in some caises, it is risky in two ways:
1) You are ordering by whichever field is first. If the column order changes, the order by changes. Stating the column explicitly is safer.
2) The rownum <= upperlimit is not correct as it depends on the order by. This is functionally wrong.

See the Ask Tom article again, in particular the section titled "Pagination with ROWNUM". He shows the pattern to use is:
 
GeeCON Prague 2014
 
subject: How to define ResultSet limit by PL/SQl in Oracle database