Connect by in Oracle implemented in SQL Server

Source: Internet
Author: User

1. In Oracle:

Select org_id

From organizations

Start with org_id=: org_id

Connect by Prior org_id=supervision_org_id

ORDER BY supervision_org_id

2. SQL Functiong:

Functionno Subfunctionno Subfunctiontype

Systemadmin Privilege 0

Systemadmin Subsysadmin 1 (represented by tree branch)

Subsysadmin subsys1 0 (Leaf)

Subsysadmin Subsys2 0

Subsysadmin User 1

User Userone 0

Please show the number of functions that this hierarchy outputs (you can use connect by to do this in Oracle)

Thanks for the first.

3. SQL Statement implementation:

Example:

Declare @a table (tc_id int,tc_pid int,tc_name varchar (200))

Insert @a VALUES (1, 0, ' China ')

Insert @a values (2,0, ' USA ')

Insert @a values (3,0, ' Canada ')

Insert @a values (4,1, ' Beijing ')

Insert @a values (5,1, ' Shanghai ')

Insert @a values (6,1, ' Jiangsu ')

Insert @a values (7,6, ' Suzhou ')

Insert @a values (8,7, ' Changshu ')

Insert @a values (9,6, ' Nanjing ')

Insert @a values (10,6, ' Wuxi ')

Insert @a values (11,2, ' New York ')

Insert @a values (12,2, ' San Francisco ')

Declare @tmp1 table (tc_id int,tc_pid int,tc_name varchar (), Lev Int)

Insert @tmp1 Select *,1 from @a where tc_id=1

While exists (select 1 to @a A, @tmp1 b where a.tc_pid=b.tc_id and a.tc_id not in (select tc_id from @tmp1))

Insert @tmp1 Select a.*,1 from @a A, @tmp1 b where a.tc_pid=b.tc_id and a.tc_id isn't in (select tc_id from @tmp1)

SELECT * FROM @tmp1

Note : Please pay attention to the triple programming Tutorials section for more wonderful articles .

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.