Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
somenath chatterjee
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The SQL to be used is database specific. Can you please mention the database [like MySQL or Oracle] you are using?
 
Paul Clapham
Sheriff
Pie
Posts: 20758
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somenath,
Oracle provides a keyword called "rownum" that allows you to return each piece. This Ask Tom article gives a sample.
 
karthikeyan Chockalingam
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic