Code Analysis of an Oracle recursive query instance

Source: Internet
Author: User

Oracle DatabaseOfRecursive queryIt is widely used. For recursive queries, we must first define the recursive function, its parameters and return values, and then implement the query. This article introduces this process through an example of code. Next we will introduce it.

I. Tree table structure:

Node ID parent ID node name

Ii. formula:

 
 
  1. Select node ID, node name, level
  2.  
  3. From table
  4.  
  5. Connect by prior node ID = parent node ID
  6.  
  7. Start with parent node ID = node Value

Note:

1. The common tree structure is the company's organization, region ......

2. Find the structure above the node ID or above, and change the order of "Node ID = parent node ID.

3. Level is a special field of Oracle, indicating the meaning of "layer. The next node of the current node ID is "1 ".

Test SQL:

Description 1. Calculate all subnodes and layers below 002 (or above) (dynamic: always starts from 1), but does not include itself.

NOTE 2: If you want to find a node larger than 002, you can change the conditions on both sides of "connect by prior topno = no" and "=.

 
 
  1. select departno,departname,level  
  2.  
  3. from dept  
  4.  
  5. connect by prior departno=topno 
  6.  
  7. start with topno='002'; 

Test data:

 
 
  1. Create table Dept (
  2.  
  3. Optional no varchar2 (10 ),
  4.  
  5. Invalid name varchar2 (20 ),
  6.  
  7. TopNo varchar2 (10 ));
  8.  
  9. Insert into Dept values ('001', 'board of directors ', '0 ');
  10.  
  11. Insert into Dept values ('002 ', 'President office', '001 ');
  12.  
  13. Insert into Dept values ('003 ', 'Finance Department', '001 ');
  14.  
  15. Insert into Dept values ('004 ', 'marketing Department', '002 ');
  16.  
  17. Insert into Dept values ('005 ', 'public relations', '002 ');
  18.  
  19. Insert into Dept values ('006 ', 'sales Department', '002 ');
  20.  
  21. Insert into Dept values ('007 ', 'sales office', '006 ');
  22.  
  23. Insert into Dept values ('008 ', 'business expansion data', '004 ');
  24.  
  25. Insert into Dept values ('009', 'sales core', '007 ');

Forward query, for example:

 
 
  1. select distinct departno,departname,level  
  2.  
  3. from dept  
  4.  
  5. connect by prior topno=departno 
  6.  
  7. start with  
  8.  
  9. departno='005'; 

In fact, the query result is 005,002,001 for both the user and the superior.

Summary:The key to recursion is to define a recursive function. The key to a recursive function is to define its parameters and return values. Parameters are the most important. How can we define parameters? What is the process of recursion? Find its parent id based on its id, find its child based on its parent id, and find its child based on its child. Therefore, the analysis of this parameter must be an id, because only the input id knows how to find this id.

We have introduced so much about recursive queries in Oracle databases. Thank you for your support!

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.