declare @tmp table (Code varchar(15) , Parent varchar(15)) insert @tmp select Code,Parent from test where Parent = '102N' while @@rowcount > 0 begin insert @tmp select Code,Parent from test where Parent in (select Code from @tmp) and Code not in (select Code from @tmp) end select * from @tmp
102NHA1 102N
102NHA2 102N
102NHB1 102NHA1
102NHC1 102NHB1
有勞 當102N有子類 102N根節點紀錄刪除
insert @tmp
select Code,Parent from test where Parent = '102N'
while @@rowcount > 0 begin
insert @tmp
select Code,Parent from test where Parent in (select Code from @tmp) and Code not in (select Code from @tmp)
end
select * from @tmp
請幫忙給我解釋下!