您可以使用while循環來獲取父親和子女7,8,9節點將它們存儲在臨時表:
declare @myInvId int
declare @father_counter int
declare @children_counter int
declare @temp table (Id int, InvID int, InvDate datetime, OriginalInvID int, FilterDesc nvarchar(100))
declare @fathers table(id int, status int)
declare @children table(id int, status int)
insert into @temp
select 1, 1, '2017-03-01 00:00:00.000', 0, 'First Inv Created'
union all select 2, 2, '2017-03-06 00:00:00.000', 1, 'Link ID'
union all select 3, 3, '2017-03-01 00:00:00.000', 0, 'First Inv Created'
union all select 4, 4, '2017-03-03 00:00:00.000', 3, 'Link ID'
union all select 5, 5, '2017-03-06 00:00:00.000', 4, 'Second Inv Created on top of MapID'
union all select 6, 6, '2017-03-01 00:00:00.000', 0, 'First Inv Created'
union all select 7, 7, '2017-03-03 00:00:00.000', 6, 'Link ID'
union all select 8, 8, '2017-03-05 00:00:00.000', 7, 'Second In Created on top of Map Id'
union all select 9, 9, '2017-03-06 00:00:00.000', 8, 'Second Inv Created on top of Map Id'
--set input parameter
set @myInvId = 8
--init counters
set @father_counter = 1
set @children_counter = 1
--insert starting node
insert into @fathers select InvID,0 from @temp where OriginalInvID = @myInvId
insert into @children select OriginalInvID,0 from @temp where InvID = @myInvId
--loop on table
while (@father_counter + @children_counter) > 0
begin
--fetch father nodes
insert into @fathers select InvID, 1 from @temp where OriginalInvID in (select id from @fathers where status=0)
--update fathers' status
update @fathers set status = 2 where status = 0
update @fathers set status = 0 where status = 1
--fetch children nodes
insert into @children select OriginalInvID, 1 from @temp where InvID in (select id from @children where status=0)
--update children's status
update @children set status = 2 where status = 0
update @children set status = 0 where status = 1
--update counters
select @father_counter = count(*) from @fathers where status = 0
select @children_counter = count(*) from @children where status = 0
end
select @myInvId
union
select id from @fathers
union
select c.id from @children c inner join @temp t on c.id = t.InvID
你需要的SQL查詢?或者linq查詢?或者是什麼? –
這顯然是標籤錯誤這是沒有jquery我已經刪除它 – guradio