现在的位置: 首页 > 综合 > 正文

sql读取 获取子节点 父节点

2012年09月05日 ⁄ 综合 ⁄ 共 461字 ⁄ 字号 评论关闭

函数

CREATE FUNCTION GetChildren (@id varchar(20))
RETURNS @t table(id varchar(20))  AS 
BEGIN
    insert @t select subid from tree where parentid = @id
    while @@rowcount > 0
        insert @t select a.subid from tree as a inner join @t as b
        on a.parentid = b.id and a.subid not in(select id from @t)
   return
END

 

函数 GetChildren 获取子节点
函数 GetParent 获取父节点

 

select top 8 * from Pdt_Detail where CompanySortId = 1 or CompanySortId in (select * from GetChildren(1))

select * from Pdt_Detail where CompanySortId in(select * from GetChildren(38))

抱歉!评论已关闭.