Detailed Explanation of oracle SQL Recursion
- 2021-07-03 01:00:47
- OfStack
If you need to use SQL recursive statement in oracle database, how should you write it? The following is an example of using SQL recursive statement in oracle for your reference.
Examples:
Oracle SQL Recursive Query Statement:
1. Table mechanism
2. Recursively find all child nodes under the parent node org_id as C60000000001:
3. Recursively find the child node org_id for all parent nodes under C6000000000001:
Examples:
pid id
a b
a c
a e
b b1
b b2
c c1
e e1
e e3
d d1
Specify pid=a , elected
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 mechanism
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 find all child nodes under the parent node org_id as C60000000001:
select * from comm_org_subjection a
start with a.org_id='C6000000000001'
connect by prior a.org_subjection_id=a.father_org_id
3. Recursively find the child node org_id for all parent nodes under C6000000000001:
select org_id from comm_org_subjection a
start with a.org_id='C6000000000001'
connect by prior a.father_org_id=a.org_subjection_id