日期:2014-05-18 浏览次数:20430 次
-->Title:查找指定節點下的子結點 if object_id('Uf_GetChildID')is not null drop function Uf_GetChildID go create function Uf_GetChildID(@ParentID int) returns @t table([departmentId] int) as begin insert @t select [departmentId] from [tbl] where [parentId]=@ParentID while @@rowcount<>0 begin insert @t select a.[departmentId] from [tbl] a inner join @t b on a.[parentId]=b.[departmentId] and not exists(select 1 from @t where [departmentId]=a.[departmentId]) end return end go declare @id varchar(10) set @id='' select @id=@id+cast([departmentId] as varchar) from (select [departmentId]=1 union all select * from dbo.Uf_GetChildID(1))a print @id select @id as [departmentId] /* departmentId 1234 */ 按照你的方式显示