Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / database

Post on how to do Paging in Oracle

1.00/5 (1 vote)
2 Feb 2011CPOL 12.9K  
Oracle Paging
It is always difficult to handle paging in Oracle as we need to put an inner query with rownum, and we give a where condition in the outer query with that pseudo rownum column.

This is the conventional way of doing it:
SQL
select * from (select rownum,fld1,fld2,fld3... from bigtable where <<CONDITION>> ) where rownum between 1 to 100

and we say rownum between 101 to 200, and so on...

We can rewrite this in the following way, but we need a running number column in our table. Let's say our running number column name is SYSID. Then, we can write this like:
SQL
Select * from bigtable where <<CONDITION>> and SYSID>=1 and rownum between 1 to 100


The next time we take the greatest sysid from the above return query and execute next step like:
SQL
Select * from bigtable where <<CONDITION>> and SYSID>PREVIOUS GREATEST SYSID and rownum between 1 to 100


When we get a rowcount less than 100, that means we are finished with fetching all the rows.

Limitations

Order By, Group By clauses will not work with this.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)