Tree Structure Query without CTE

Source: Internet
Author: User
-- Create test table IFOBJECT_ID (wx_test_dep) Partition (depIdINTPRIMARYKEY, pDepIdINT, depNameNVARCHAR (20) GO -- insert test data into partition (, Wuhan University) INSERTINTOwx_test_dep

-- Create the test table if OBJECT_ID ('wx _ test_dep ') is not nullddrop TABLE wx_test_depGOCREATE TABLE wx_test_dep (depId int primary key, pDepId INT, depName NVARCHAR (20 )) GO -- INSERT test data insert into wx_test_dep VALUES (1, 0, 'wuhan Emy ') insert into wx_test_dep

-- Create the test table if OBJECT_ID ('wx _ test_dep ') is not nullddrop TABLE wx_test_depGOCREATE TABLE wx_test_dep (depId int primary key, pDepId INT, depName NVARCHAR (20 )) GO -- INSERT test data insert into wx_test_dep VALUES (, 'wuhan Emy ') insert into wx_test_dep VALUES (, '') insert into wx_test_dep VALUES (, 'wuhan University Logistics Department ') insert into wx_test_dep VALUES (, 'technical division of the Armed Police Department ') insert into wx_test_dep VALUES (, 'Armed police Supply Division') insert into wx_test _ Dep VALUES (6, 2, 'huake college studeny') insert into wx_test_dep VALUES (7, 6, 'huake College Students ') insert into wx_test_dep VALUES (8, 6, 'huake college students ') -- Wuhan University Logistics Department -- ---- Wuhan University Logistics Department Technical Department -- ---- Wuhan University Logistics Department supply department -- huake University -- huake university department -- ---- huake university department technical department -- ---- huake university department teaching department -- SELECT * FROM wx_test_dep wtd -- find all sub-departments of the Chinese Emy of Science and Technology/* 20 62 huake university department 76 huake University Department Technical Department 86 huake University Department Teaching Department **/DECLARE @ t TABLE (depId INT, pDepId INT, depName NVARCHAR (20) insert into @ tSELECT * FROM wx_test_de P wtd WHERE wtd. depId = 2 while exists (SELECT 1 FROM wx_test_dep w where exists (SELECT 1 FROM @ t WHERE w. pDepId = t. depId -- AND w. depId! = T. depId) and not exists (SELECT 1 FROM @ t WHERE w. depId = t. depId) begininsert into @ tSELECTwtd. depId, wtd. pDepId, wtd. depNameFROMwx_test_dep wtd where exists (SELECT 1 FROM @ t WHERE wtd. pDepId = t. depId -- AND wtd. depId! = T. depId) and not exists (SELECT 1 FROM @ t WHERE wtd. depId = t. depId) ENDSELECT * FROM @ t

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.