Use of oracle rownum statements

Source: Internet
Author: User

I believe everyone has some knowledge about the ORACLE ROWNUM statement. The following describes how to use the ORACLE ROWNUM statement in detail, hoping to help you.

The pseudo column ROWNUM contains the current row number. Many people will find that the following method does not work when they first try to return a sub-set of records in the middle of the table:

Select * from all_objects where rownum between 30 and 49;

This method does not work because the ROWNUM column is applied to the record only when the record is retrieved or filtered. The first row is thrown when it is retrieved because its ROWNUM is 1. Then, the next row is taken out; it will also be thrown because it is the new "1", and so on until all rows are used. This query does not return any records. The solution is to retrieve 1 to 30 rows of records when you see records between 30 and 50:

Select * from all_objects where rownum <= 49;

Then, you can use it as a subquery and filter out all records before the start point (note that I must provide an alias for "rownum" to compile ):

Select * from (select rownumr, all_objects. * from all_objects where rownum <=

49) t

Where t. r> = 30;

To ensure its efficiency, you may wish to use the bound variable for this restriction. This will use all these types of requests to be exactly the same literally, eliminating the re-resolution of the query when each request has different ranges:

Select * from (select rownumr, all_objects. * from all_objects where rownum <=: min) t
Where t. r >=: max;
Note that the oracle rownum statement is calculated after sorting, so the order by clause is used to obtain a new record ORDER. However, the rule-based optimizer uses ROWNUM for "short-circuit" queries and returns records to the next part of queries when the ROWNUM clause is satisfied.

Oracle paging query statements can be applied in the format given in this article.
 

Oracle paging query statements

Oracle index type

How to create an Oracle Index

Connect to the oracle database using JAVA

C # connect to the Oracle database to query data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.