表1 t1
t1_id,t1_parentid,t1_name
   表2 t2
t2_id,t1_id,t2_namet1是多层的树.要求是根据t1的id 查询出所有的t2_name包括所有孙子.

解决方案 »

  1.   

    /*
    标题:查询指定节点及其所有子节点的函数
    作者:爱新觉罗.毓华 
    时间:2008-05-12
    地点:广东深圳
    */create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
    insert into tb values('001' , null  , '广东省')
    insert into tb values('002' , '001' , '广州市')
    insert into tb values('003' , '001' , '深圳市')
    insert into tb values('004' , '002' , '天河区')
    insert into tb values('005' , '003' , '罗湖区')
    insert into tb values('006' , '003' , '福田区')
    insert into tb values('007' , '003' , '宝安区')
    insert into tb values('008' , '007' , '西乡镇')
    insert into tb values('009' , '007' , '龙华镇')
    insert into tb values('010' , '007' , '松岗镇')
    go--查询指定节点及其所有子节点的函数
    create function f_cid(@ID varchar(3)) returns @t_level table(id varchar(3) , level int)
    as
    begin
      declare @level int
      set @level = 1
      insert into @t_level select @id , @level
      while @@ROWCOUNT > 0
      begin
        set @level = @level + 1
        insert into @t_level select a.id , @level
        from tb a , @t_Level b
        where a.pid = b.id and b.level = @level - 1
      end
      return
    end
    go--调用函数查询001(广东省)及其所有子节点
    select a.* from tb a , f_cid('001') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    001  NULL 广东省
    002  001  广州市
    003  001  深圳市
    004  002  天河区
    005  003  罗湖区
    006  003  福田区
    007  003  宝安区
    008  007  西乡镇
    009  007  龙华镇
    010  007  松岗镇(所影响的行数为 10 行)
    */--调用函数查询002(广州市)及其所有子节点
    select a.* from tb a , f_cid('002') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    002  001  广州市
    004  002  天河区(所影响的行数为 2 行)
    */--调用函数查询003(深圳市)及其所有子节点
    select a.* from tb a , f_cid('003') b where a.id = b.id order by a.id
    /*
    id   pid  name       
    ---- ---- ---------- 
    003  001  深圳市
    005  003  罗湖区
    006  003  福田区
    007  003  宝安区
    008  007  西乡镇
    009  007  龙华镇
    010  007  松岗镇(所影响的行数为 7 行)
    */drop table tb
    drop function f_cid
      

  2.   

    --查询指定节点及其所有子节点的函数
    create function f_cid(@t1_id varchar(3)) returns @t_level table(t1_id varchar(3) , level int)
    as
    begin
      declare @level int
      set @level = 1
      insert into @t_level select @t1_id , @level
      while @@ROWCOUNT > 0
      begin
        set @level = @level + 1
        insert into @t_level select a.t1_id , @level
        from t1 a , @t_Level b
        where a.t1_parentid = b.t1_id and b.level = @level - 1
      end
      return
    end
    go--调用函数查询001(例如001)及其所有子节点 
    select m.* , n.t2_name from
    (select a.* from tb a , f_cid('001') b where a.t1_id = b.t1_id order by a.t1_id ) m , t2 n
    where m.t1_id = n.t1_id
      

  3.   


    --首先查找该节点下所有子接点
    create function f_cid(@t1_id int)
    returns varchar(500)
    as
    begin
         declare @t table(t1_id int,t1_parentid int,t1_name varchar(10),lev int)
         declare @lev int
         set @lev=1
         insert into @t select *,@lev from  t1 where t1_id=@t1_id
         while(@@rowcount>0)
         begin
              set @lev=@lev+1
              insert into @t select a.*,@lev from t1 a,@t b
              where a.t1_parentid=b.t1_id and b.lev=@lev-1
         end
         declare @cids varchar(500)
         select @cids=isnull(@cids+',','')+ltrim(t1_name) from @t order by lev
         return @cids
    end
    select *,dbo.f_cid(t1_id) from t2
     
      

  4.   

    --测试数据
    CREATE TABLE tb(ID char(3),PID char(3),Name nvarchar(10))
    INSERT tb SELECT '001',NULL ,'山东省'
    UNION ALL SELECT '002','001','烟台市'
    UNION ALL SELECT '004','002','招远市'
    UNION ALL SELECT '003','001','青岛市'
    UNION ALL SELECT '005',NULL ,'四会市'
    UNION ALL SELECT '006','005','清远市'
    UNION ALL SELECT '007','006','小分市'
    GO--查询指定节点及其所有子节点的函数
    CREATE FUNCTION f_Cid(@ID char(3))
    RETURNS @t_Level TABLE(ID char(3),Level int)
    AS
    BEGIN
    DECLARE @Level int
    SET @Level=1
    INSERT @t_Level SELECT @ID,@Level
    WHILE @@ROWCOUNT>0
    BEGIN
    SET @Level=@Level+1
    INSERT @t_Level SELECT a.ID,@Level
    FROM tb a,@t_Level b
    WHERE a.PID=b.ID
    AND b.Level=@Level-1
    END
    RETURN
    END
    GO--调用函数查询002及其所有子节点
    SELECT a.*
    FROM tb a,f_Cid('002') b
    WHERE a.ID=b.ID
    /*--结果
    ID   PID  Name       
    ------ ------- ---------- 
    002  001  烟台市
    004  002  招远市
    --*/
      

  5.   


    8.2.4 查找指定节点的所有父节点的示例函数.sql
    CREATE FUNCTION f_Pid(@ID char(3))
    RETURNS @t_Level TABLE(ID char(3),Level int)
    AS
    BEGIN
    DECLARE @Level int
    SET @Level=1
    INSERT @t_Level SELECT @ID,@Level
    WHILE @@ROWCOUNT>0
    BEGIN
    SET @Level=@Level+1
    INSERT @t_Level SELECT a.PID,@Level
    FROM tb a,@t_Level b
    WHERE a.ID=b.ID
    AND b.Level=@Level-1
    END
    RETURN
    END
    GO
    --上面的用户定义函数可以处理一个节点有多个父节点的情况,对于标准的树形数据而言,由于每个节点仅有一个父节点,所以也可以通过下面的用户定义函数实现查找标准树形数据的父节点。
    CREATE FUNCTION f_Pid(@ID char(3))
    RETURNS @t_Level TABLE(ID char(3))
    AS
    BEGIN
    INSERT @t_Level SELECT @ID
    SELECT @ID=PID FROM tb
    WHERE ID=@ID
    AND PID IS NOT NULL
    WHILE @@ROWCOUNT>0
    BEGIN
    INSERT @t_Level SELECT @ID
    SELECT @ID=PID FROM tb
    WHERE ID=@ID
    AND PID IS NOT NULL
    END
    RETURN
    END
      

  6.   


    树形数据深度排序处理示例(递归法)
    --测试数据
    CREATE TABLE tb(ID char(3),PID char(3),Name nvarchar(10))
    INSERT tb SELECT '001',NULL ,'山东省'
    UNION ALL SELECT '002','001','烟台市'
    UNION ALL SELECT '004','002','招远市'
    UNION ALL SELECT '003','001','青岛市'
    UNION ALL SELECT '005',NULL ,'四会市'
    UNION ALL SELECT '006','005','清远市'
    UNION ALL SELECT '007','006','小分市'
    GO--广度搜索排序函数
    CREATE FUNCTION f_Sort(@ID char(3)=NULL,@sort int=1)
    RETURNS @t_Level TABLE(ID char(3),sort int)
    AS
    BEGIN
    DECLARE tb CURSOR LOCAL
    FOR
    SELECT ID FROM tb
    WHERE PID=@ID
    OR(@ID IS NULL AND PID IS NULL)
    OPEN TB
    FETCH tb INTO @ID
    WHILE @@FETCH_STATUS=0
    BEGIN
    INSERT @t_Level VALUES(@ID,@sort)
    SET @sort=@sort+1
    IF @@NESTLEVEL<32 --如果递归层数未超过32层(递归最大允许32层)
    BEGIN
    --递归查找当前节点的子节点
    INSERT @t_Level SELECT * FROM f_Sort(@ID,@sort)
    SET @sort=@sort+@@ROWCOUNT  --排序号加上子节点个数
    END
    FETCH tb INTO @ID
    END
    RETURN
    END
    GO--显示结果
    SELECT a.*
    FROM tb a,f_Sort(DEFAULT,DEFAULT) b
    WHERE a.ID=b.ID
    ORDER BY b.sort
    /*--结果
    ID   PID   Name       
    ------ --------- ---------- 
    001  NULL 山东省
    002  001   烟台市
    004  002   招远市
    003  001   青岛市
    005  NULL 四会市
    006  005   清远市
    007  006   小分市
    --*/