;with t AS
(
SELECT id FROM dbo.tkf_OpDepartment where id =1
union all
select b.id
from dbo.tkf_OpDepartment as b,t where b.ParentID=t.id
)
select ltrim(id) as col from t order by id
—————————————————-本级及子集————————————
;with t AS
(
SELECT *,[level]=cast(” as varchar(50)) FROM tkf_OpDepartment where ParentID =0
union all
select b.*,[level]=cast(‘,’+ltrim(b.ParentID)+[level] as varchar(50))
from tkf_OpDepartment as b,t where b.ParentID=t.id
)
select ltrim(id)+[level] as col from t order by id