需求是这样的。假如说有一文章类别表,如下, 
ClassCode varchar(16) ClassName varchar(256) 要求写一函数实现文章类别的三级分类, 
如ClassCode=100,同级的下一条记录的ClassCode为101,那么他的下级分类为100100。 
如下所描述。 
100 
  100100 
        100100100 
        100100101 
  100101 
        100101100 
        100101101 
101 

解决方案 »

  1.   

    *
    标题:查询各节点的父路径函数
    作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)  
    时间:2008-05-12
    地点:广东深圳
    *//*
    原始数据及要求结果如下:
    --食品 
      --水果 
        --香蕉 
        --苹果    
      --蔬菜 
        --青菜
    id          pid         name                 
    ----------- ----------- -------------------- 
    1           0           食品
    2           1           水果
    3           1           蔬菜
    4           2           香蕉
    5           2           苹果
    6           3           青菜要求得到各节点的父路径即如下结果:
    id  pid name  路径                         
    --- --- ----- ---------------
    1   0   食品  食品
    2   1   水果  食品,水果
    3   1   蔬菜  食品,蔬菜
    4   2   香蕉  食品,水果,香蕉
    5   2   苹果  食品,水果,苹果
    6   3   青菜  食品,蔬菜,青菜 
    */create table tb (id int , pid int , name nvarchar(20)) 
    insert into tb values(1 , 0 , '食品')
    insert into tb values(2 , 1 , '水果')
    insert into tb values(3 , 1 , '蔬菜')
    insert into tb values(4 , 2 , '香蕉')
    insert into tb values(5 , 2 , '苹果')
    insert into tb values(6 , 3 , '青菜')
    go--查询各节点的父路径函数
    create function f_pid(@id int) returns varchar(100)
    as
    begin
      declare @re_str as varchar(100)
      set @re_str = ''
      select @re_str = name from tb where id = @id
      while exists (select 1 from tb where id = @id and pid <> 0)
        begin
          select @id = b.id , @re_str = b.name + ',' + @re_str from tb a , tb b where a.id = @id and a.pid = b.id
        end
      return @re_str
    end
    goselect * , dbo.f_pid(id) 路径 from tb order by iddrop table tb
    drop function f_pidSQL code
    /*
    标题:查询所有节点及其所有子节点的函数
    作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开) 
    时间:2009-04-12
    地点:广东深圳
    */--生成测试数据 
    create table tb(id varchar(10),pid varchar(10)) 
    insert into tb select 'a', null 
    insert into tb select 'b', 'a' 
    insert into tb select 'c', 'a' 
    insert into tb select 'd', 'b' 
    insert into tb select 'e', 'b' 
    insert into tb select 'f', 'c' 
    insert into tb select 'g', 'c' 
    go --创建用户定义函数 
    create function f_getchild(@id varchar(10)) returns varchar(8000) 
    as 
    begin 
      declare @i int , @ret varchar(8000) 
      declare @t table(id varchar(10) , pid varchar(10) , level int) 
      set @i = 1 
      insert into @t select id , pid , @i from tb where id = @id 
      while @@rowcount <> 0 
      begin 
        set @i = @i + 1 
        insert into @t select a.id , a.pid , @i from tb a , @t b where a.pid = b.id and b.level = @i - 1
      end 
      select @ret = isnull(@ret , '') + id + ',' from @t 
      return left(@ret , len(@ret) - 1)
    end 
    go --执行查询 
    select id , children = isnull(dbo.f_getchild(id) , '') from tb group by id
    go --输出结果 
    /* 
    id         children     
    ---------- -------------
    a          a,b,c,d,e,f,g
    b          b,d,e
    c          c,f,g
    d          d
    e          e
    f          f
    g          g(所影响的行数为 7 行)*/ --删除测试数据 
    drop function f_getchild 
    drop table tb
      

  2.   

    BOM之深度排序
    --测试数据   深度排序     
      DECLARE   @t   TABLE(ID   char(3),PID   char(3),Name   nvarchar(10))   
      INSERT   @t   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','小分市'   
        
      --深度排序显示处理   
      --生成每个节点的编码累计(相同当单编号法的编码)   
      DECLARE   @t_Level   TABLE(ID   char(3),Level   int,Sort   varchar(8000))   
      DECLARE   @Level   int   
      SET   @Level=0   
      INSERT   @t_Level   SELECT   ID,@Level,ID   
      FROM   @t   
      WHERE   PID   IS   NULL   
      WHILE   @@ROWCOUNT>0   
      BEGIN   
      SET   @Level=@Level+1   
      INSERT   @t_Level   SELECT   a.ID,@Level,b.Sort+a.ID   
      FROM   @t   a,@t_Level   b   
      WHERE   a.PID=b.ID   
      AND   b.Level=@Level-1   
      END   
        
      --显示结果   
      SELECT   a.*   
      FROM   @t   a,@t_Level   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       小分市   
      --*/--查询指定节点及其所有子节点的函数   
      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     招远市   
      --*/ --测试数据
    DECLARE @t TABLE(ID char(3),PID char(3),Name nvarchar(10))
    INSERT @t 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','小分市'--深度排序显示处理
    --生成每个节点的编码累计(相同当单编号法的编码)
    DECLARE @t_Level TABLE(ID char(3),Level int,Sort varchar(8000))
    DECLARE @Level int
    SET @Level=0
    INSERT @t_Level SELECT ID,@Level,ID
    FROM @t
    WHERE PID IS NULL
    WHILE @@ROWCOUNT>0
    BEGIN
        SET @Level=@Level+1
        INSERT @t_Level SELECT a.ID,@Level,b.Sort+a.ID
        FROM @t a,@t_Level b
        WHERE a.PID=b.ID
            AND b.Level=@Level-1
    END--显示结果
    SELECT SPACE(b.Level*2)+'|--'+a.Name
    FROM @t a,@t_Level b
    WHERE a.ID=b.ID
    ORDER BY b.Sort
    /*--结果
    |--山东省
      |--烟台市
        |--招远市
      |--青岛市
    |--四会市
      |--清远市
        |--小分市
    --*/
      

  3.   

    我要设计的数据和这些有点不一样,并不是深度遍历,要做的是实现文章的三级分类,而级别是从ClassCode中获取的,我只设计三级。也就是说第一级的编号是三位数,二级的编号是六位数,三级的编号是9位数。从数字中,可以看到他的级别。