-- 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