ORACLE renewal history Query

Source: Internet
Author: User

ORACLE renewal history Query

ORACLE supports regular query operations by using the CTE internal audit method, and also has its own unique query method. In ORACLE, this method is called continuous data query.

This example illustrates the two query methods through a simple example.

Number of rows:

CREATE TABLE TBL_TEST( ID NUMBER,  NAME VARCHAR2(100),  PID NUMBER); /BEGININSERT INTO TBL_TEST(ID,NAME,PID) VALUES('1','10','0');INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('2','11','1');INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('3','20','0');INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('4','12','1');INSERT INTO TBL_TEST(ID,NAME,PID) VALUES('5','121','2');END; /

Requirement: ID = all the sublinks under the 1 Link (the values are 1, 2, 4, and 5 in the table)

The method for ORACLE-specific volume tracing query is as follows:

SELECT * FROM TBL_TestSTART with id = 1 -- start row connect by prior id = pid; -- prior indicates the previous row (parent row)
CTE mode:
WITH cte(ID,NAME,PID) AS(    SELECT ID,NAME,PID    FROM TBL_TEST    WHERE ID = 1UNION ALL    SELECT b.ID,b.NAME,b.PID    FROM cte a    INNER JOIN TBL_TEST b ON a.id = b.PID)SELECT * FROM cte;
The above two methods have all found positive results.

The Start With of the first method is equivalent to the part above union all of the second method. connect by is equivalent to the part below union all.

The CTE method is portable. It is also supported by SQL SERVER and DB2.

You can refer to the following articles for more examples of the CTE method:

SQL audit validation

Checking your initial experiences

Related Article

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.