if not object_id('tb') is null drop table tb Go Create table tb([id] int,[parentid] int,[name] varchar(10)) Insert tb select 1,0,null union all select 2,0,null union all select 3,1,null union all select 4,3,null union all select 5,2,null Go -->Title:查找指定節點下的子結點 if object_id('Uf_GetChildID')is not null drop function Uf_GetChildID go create function Uf_GetChildID(@ParentID int) returns @t table(ID int) as begin insert @t select ID from tb where ParentID=@ParentID while @@rowcount<>0 begin insert @t select a.ID from tb a inner join @t b on a.ParentID=b.id and not exists(select 1 from @t where id=a.id)
SQL code: declare @t varchar(5000) set @t='' select @t=@t+',[取消分析-'+quXiaoFenXi+']=sum(case when [quXiaoFenXi]='+quotename([quXiaoFenXi],'''')+' then shangPinZongShu else 0 end)' from tb_gyyD ......