SQL 树状结构表中查出所所有父级/子级

通过调整绿色区域,改变查询子、父级。注意分号,注意取完立即赋值到临时表。

;with parentData as
		(
			 select ID,ParentId,Name from PJ_ScheduleItem where ID=@ID
			 union all
			 select b.ID,b.ParentId,b.Name from  parentData a,   PJ_ScheduleItem b  
			 where a.ParentId=b.ID  and b.Status= 0
		)

		select * into #pd from parentData;	

  

原文地址:https://www.cnblogs.com/mamaxiaoling/p/14430253.html