Use of connectby... startwith... in Oracle

Source: Internet
Author: User
I. Syntax: select * fromsome_table [where condition 1] connectby [condition 2] startwith [Condition 3]. The order of the connectby and startwith statements does not affect the query results, [where condition 1] is optional. [Where condition 1], [condition 2], and [Condition 3] have different scopes.

I. Syntax: select * from some_table [where condition 1] connect by [condition 2] start with [Condition 3]; the order of the connect by and start with statements does not affect the query results. [where condition 1] is not required. [Where condition 1], [condition 2], and [Condition 3] have different scopes.

I. Syntax
Statement: select * from some_table [where condition 1] connect by [condition 2] start with [Condition 3];
The order of the connect by and start with statements does not affect the query results. [where condition 1] is not required.
[Where condition 1], [condition 2], and [Condition 3] have different scopes:

[Where condition 1] is used to filter records selected based on "connect by [condition 2] start with [Condition 3]". It is used to filter a single record, tree structure is not considered;

[Condition 2] specify the condition for constructing the tree and the filtering condition for the tree branch. The filter executed here filters out the records that meet the condition and all its subnodes;

[Condition 3] conditions are defined as the search start point. Top-down search conditions are defined as root nodes, and bottom-up search conditions are defined as leaf nodes;

Example:

Create table AA
(
Id number not null,
Pid number,
XM VARCHAR2 (10) not null
);

Alter table AA
Add constraint pk1_aa_id primary key (ID );

Alter table AA
Add constraint FK_AA_PID foreign key (PID)
References AA (ID );

The data in table AA is as follows:

ID PID XM

1
2 1 B1
3 1 B2
4 2 C1
5 2 C2
6 3 C3
7 3 C4

Query all subnode records with ID = 1

Select id, xm from aa t connect by prior t. id = t. pid start with t. id = 1;

Note:

Prior t. id = t. pid is used to search for all child nodes from the parent node;

Prior t. pid = t. id is used to search for all parent nodes from the current node;

Ii. Execution Principle
The execution principle of tconnect by... sart with... can be described by executing the following program and calling the Stored Procedure RECURSE:

/* Traverse each record in the table and compare whether the condition after start with is met. If not, continue the next record,
If yes, use the record as the root node and call RECURSE () to Recursively search for the subnodes under the node,
This loop continues until all records in the entire table are traversed. */
For rec in (select * from some_table) loop
If FULLFILLS_START_WITH_CONDITION (rec) then
RECURSE (rec, rec. child );
End if;
End loop;

/* Find the sub-node storage process */
Procedure RECURSE (rec in MATCHES_SELECT_STMT, new_parent IN field_type) is
Begin
APPEND_RESULT_LIST (rec);/* Add records to the result set */
/* Traverse all records in the table again and compare whether the conditions after connect by are met. If not, continue the next one,
If yes, use the record as the root node and call RECURSE () to Recursively search for the subnodes under the node,
This loop continues until the leaf node is found. */
For rec_recurse in (select * from some_table) loop
If FULLFILLS_CONNECT_BY_CONDITION (rec_recurse.child, new_parent) then
RECURSE (rec_recurse, rec_recurse.child );
End if;
End loop;
End procedure RECURSE;

I spoke with egg at JavaEye.

Http://maruibenze.javaeye.com/blog/208541

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.