Oracle tree SQL query example

Source: Internet
Author: User

Use this SQL statement

select  * from tree

View the raw data as follows:

The following figure shows the result of a tree query (including the fields root, level, is_leaf, and Path ):

 

Execute the following SQL statement:

select connect_by_root(child_col) root, level , decode(connect_by_isleaf,0,'No',1,'Yes') is_leaf, sys_connect_by_path(child_col,'/') pathfrom treestart with parent_col is null connect by prior child_col=parent_col;

 

 

The focus of tree query is the start with... connect by prior... statement.
And connect_by_root, connect_by_isleaf, and sys_connect_by_path functions. Decode is a common function.

 

 

Other references: http://www.cnblogs.com/tongzhenhua/archive/2008/09/23/1297253.html

 

 

Example of a dynamic query statement:

declare n_rows number; v_sql_stmt varchar2(50); v_table_name varchar2(20); v_name varchar2(20);begin v_table_name := 'tree'; v_sql_stmt := 'select count(*) from ' || v_table_name || ' where parent_col = :1'; v_name := 'asia'; dbms_output.put_line(v_sql_stmt); execute immediate v_sql_stmt into n_rows using v_name; dbms_output.put_line('The number of rows of' || v_table_name || 'is ' || n_rows);end;

 

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.