Example of using SQL recursive statements in oracle

Source: Internet
Author: User
Tags sql primary key

How should I write SQL recursive statements in oracle databases? The following is an example of using SQL recursive statements in oracle for your reference.

Example:

Pid id
A B
A c
A e
B b1
B b2
C c1
E e1
E e3
D d1

Specify pid = a and select
A B
A c
A e
B b1
B b2
C c1
E e1
E e3
SQL statement: select parent, child from test start with pid = 'A'
Connect by prior id = pid

Oracle SQL recursive query statement:
1. Table Structure
SQL> desc comm_org_subjection
Name Null? Type
-----------------------------------------------------------------------------
ORG_SUBJECTION_ID not null VARCHAR2 (32) subkey
ORG_ID not null VARCHAR2 (32)
FATHER_ORG_ID not null VARCHAR2 (32) parent key
LOCKED_IF not null VARCHAR2 (1)
START_DATE NOT NULL DATE
END_DATE DATE
EDITION_NAMEPLATE NUMBER (8)
CODE_AFFORD_IF VARCHAR2 (1)
CODE_AFFORD_ORG_ID not null VARCHAR2 (32)
CODING_SHOW_ID NUMBER (8)
BSFLAG VARCHAR2 (1)
MODIFI_DATE DATE
CREATOR_ID VARCHAR2 (32)
CREATE_DATE DATE
CREATOR VARCHAR2 (35)

2. Recursively search for all child nodes with the parent node org_id C6000000000001:
Select * from comm_org_subjection
Start with a.org _ id = 'c6000000000001'
Connect by prior a.org _ subjection_id = a. father_org_id

3. Recursively search for all the parent nodes under the subnode org_id C6000000000001:
Select org_id from comm_org_subjection
Start with a.org _ id = 'c6000000000001'
Connect by prior a. father_org_id = a.org _ subjection_id
 

Three Common SQL insert statements

Example of batch SQL statement execution

SQL Connection Methods

Three SQL paging statements

How to define an SQL primary key

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.