Oracle tree SQL query instance analysis

Source: Internet
Author: User

Oracle tree SQL query instance analysis using this SQL statement [SQL] select * from tree to view the original data is as follows:
We want to obtain the following tree query result, as shown in
(Contains four fields: ROOT, LEVEL, IS_LEAF, and PATH ):
Run the following SQL statement: [SQL] select connect_by_root (child_col) root, level,
Decode (connect_by_isleaf, 0, 'No', 1, 'yes') is_leaf, sys_connect_by_path (child_col ,'/') path from tree www.2cto.com start with parent_col is null connect by prior child_col = parent_col; [SQL] tree query focuses on start... connect by prior .... statement [SQL] And connect_by_root, connect_by_isleaf, sys_connect_by_path,
Decode is a common function. Other references: http://www.bkjia.com/database/201205/131485.html dynamic query statement example: [SQL] declare n_rows number; v_ SQL _stmt varchar2 (50); v_table_name varchar2 (20); v_name varchar2 (20); begin www.2cto.com v_table_name: = 'tree'; v_ SQL _stmt: = 'select count (*) from' | v_table_name | 'where parent_col =: 1'; v_name: = 'asa '; explain (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;
From the blue temptation Column

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.