create table #t(sysid int,subid int)
insert #t
select 1,0 union all
select 2,1 union all
select 3,2 union all
select 4,0 union all
select 5,4 union all
select 6,4 union all
select 7,5 union all
select 8,5 union all
select 9,6 union all
select 10,6
select * into #t1 from #t where subid=0
select k.*,k1.sysid csysid from
(select a.sysid,b.sysid as bsysid
from #t1 a,#t b
where a.sysid=b.subid) k,#t k1
where k.bsysid=k1.subid
drop table #t
drop table #t1
結果
sysid bsysid csysid
1 2 3
4 5 7
4 5 8
4 6 9
4 6 10
insert t
select 1,0 union all
select 2,1 union all
select 3,2 union all
select 4,0 union all
select 5,4 union all
select 6,4 union all
select 7,5 union all
select 8,5 union all
select 9,6 union all
select 10,6
go
select a.sysid as sys,b.sysid as subsys_1 into #t from t a,t b where a.subid=0
and b.subid=a.sysid
select a.*,b.sysid subsys_2
into #t1
from #t a,t b
where a.subsys_1=b.subid
select
case when not exists (select 1 from #t1 where sys=a.sys and (subsys_1<a.subsys_1 or subsys_1=a.subsys_1 and subsys_2<a.subsys_2))
then sys else null end as sys,
case when not exists (select 1 from #t1 where sys=a.sys and subsys_1=a.subsys_1 and subsys_2<a.subsys_2)
then subsys_1 else null end as subsys_1,
subsys_2
from #t1 a
order by a.sys,a.subsys_1,a.subsys_2
declare @sysid_t varchar(2)
declare @sysid2_t varchar(2)
declare @sysid3_t varchar(2)
set @sysid_t=' '
set @sysid2_t=' '
set @sysid3_t=' '
declare temp_cursor cursor for
select sysid from tt
where subid=0
open temp_cursor
fetch next from temp_cursor
into @sysid
WHILE @@FETCH_STATUS = 0
begin
--print @sysid
--------------
declare temp_cursor2 cursor for
select sysid from tt
where subid=@sysid
open temp_cursor2
fetch next from temp_cursor2
into @sysid2
WHILE @@FETCH_STATUS = 0
begin
declare temp_cursor3 cursor for
select sysid from tt
where subid=@sysid2
open temp_cursor3
fetch next from temp_cursor3
into @sysid3
WHILE @@FETCH_STATUS = 0
begin
--print @sysid+'*************'+ @sysid2+'*************' +@sysid3
if(@sysid_t<>@sysid )
begin
insert into tt2
values(@sysid,@sysid2,@sysid3)
end
else if(@sysid2_t<>@sysid2)
begin
insert into tt2
values(' ',@sysid2,@sysid3)
end
else
begin
insert into tt2
values(' ',' ',@sysid3)
end
set @sysid_t=@sysid
set @sysid2_t=@sysid2
set @sysid3_t=@sysid3
fetch next from temp_cursor3
into @sysid3
end
CLOSE temp_cursor3
DEALLOCATE temp_cursor3
------
fetch next from temp_cursor2
into @sysid2
end
CLOSE temp_cursor2
DEALLOCATE temp_cursor2
--------------
--select sysid from tt
--where subid=@sysid
fetch next from temp_cursor
into @sysid
end
CLOSE temp_cursor
DEALLOCATE temp_cursor
go
----for test---
select *
from tt2
---------
其中表tt(sysid,subid)
表tt1(sys,subsys_1,subsys_2)
前提:此樹形最多三層
create table tree(sysid int, subid int)
insert tree select 1, 0
union
select 2, 1
union
select 3, 2
union
select 4, 0
union
select 5, 4
union
select 6, 4
union
select 7, 5
union
select 8, 5
union
select 9, 6
union
select 10, 6
WITH TreeCTE(sysid, subid, lvl, sortcol)
AS
(
SELECT sysid, subid, 0, CAST(sysid AS VARBINARY(900))
FROM tree
WHERE subid = 0
UNION ALL
SELECT E.sysid, E.subid, M.lvl+1, CAST(sortcol + CAST(E.sysid AS BINARY(4)) AS VARBINARY(900))
FROM tree AS E JOIN TreeCTE AS M ON E.subid = M.sysid
)
SELECT
REPLICATE(' | ', lvl)
+ '(' + (CAST(sysid AS VARCHAR(10))) + ') '
FROM TreeCTE
ORDER BY sortcol