CodePudding user response:
Don't know if what you said is the processing of SQL level, when it
If object_id (' tempdb for.. # # TAB ') is not null drop table TAB
The create table # TAB (ID nvarchar (50), ParentID nvarchar (50), the Name nvarchar (50))
Insert into # TAB (ID, ParentID, Name)
Select '1', '-', 'A' union all
Select '2', '1', 'B' union all
Select '3', '1', 'C' union all
Select the '4', '2', 'D' union all
Select the '5', '2', 'E' union all
Select '6', '3', 'F' union all
Select the '7', '3', 'G' union all
Select the '8', '4', 'H' union all
Select the '9', '4', 'I' union all
Select the '10', '8', 'J' union all
Select '11', '10', 'K'
; As with t (
Select ID, ParentID, Name, 1 as lv, cast (Name as nvarchar (Max)), the as path from # TAB where ParentID='-'
Union all
Select Anderson D, Amy polumbo arentID, a.N ame, as lv (t.l v + 1), (t.p ath + '-' + a.N ame) the as path
The from # TAB a inner join t on Amy polumbo arentID=t.I D
)
Select the Name of Louis vuitton, the path from t
CodePudding user response:
Not, is to use a queue to binary tree level traversal