Declare @Id Int
Set @Id = 0; ---在此修改父节点

With RootNodeCTE(D_ID,D_FatherID,D_Name,lv)
As
(
Select D_ID,D_FatherID,D_Name,0 as lv From [LFBMP.LDS].[dbo].[LDS.Dictionary] Where D_FatherID In (@Id)
Union All
Select [LFBMP.LDS].[dbo].[LDS.Dictionary].D_ID,[LFBMP.LDS].[dbo].[LDS.Dictionary].D_FatherID,[LFBMP.LDS].[dbo].[LDS.Dictionary].D_Name,lv+1 From RootNodeCTE
Inner Join [LFBMP.LDS].[dbo].[LDS.Dictionary]
On RootNodeCTE.D_ID = [LFBMP.LDS].[dbo].[LDS.Dictionary].D_FatherID
)
Select * From RootNodeCTE

;With TB([Cd_ID],[ConstituteID],[Cd_PID],[Cd_CName],lv)
as (
Select [Cd_ID],[ConstituteID],[Cd_PID],[Cd_CName],0 as lv FROM [LFBMP.Center].[dbo].[ConstituteDetail] Where [Cd_PID]=0 And [ConstituteID]=4
union all
Select A.[Cd_ID],A.[ConstituteID],A.[Cd_PID],A.[Cd_CName] ,lv+1 FROM TB inner join [LFBMP.Center].[dbo].[ConstituteDetail] as A
on TB.[Cd_ID]=A.Cd_PID
)
Select * From TB

04-28 05:23