先序遍历
classid   classname fatherid  scblevel
1          类1        -1         01
2          类1.1      1          0101
3          类1.2      1          0102
4          类1.3      1          0103
5          类2         -1        02
6          类2.1      5          0201
7          类2.1.1    6          020101
8          类2.1.1.1  7          02010101求生成scblevel的update语句。
并且还要判断fatherid为-1的情况。

解决方案 »

  1.   


    create table tb(classid int, classname varchar(20), fatherid int, scblevel varchar(20))
    insert tb
    select 1          ,'类1',        -1         ,null
    union select 2          ,'类1.1',      1          ,null
    union select 3          ,'类1.2',      1          ,null
    union select 4          ,'类1.3',      1          ,null
    union select 5          ,'类2',         -1        ,null
    union select 6          ,'类2.1',      5          ,null
    union select 7          ,'类2.1.1',    6          ,null
    union select 8          ,'类2.1.1.1',  7          ,nullupdate tb
    set scblevel=replace(replace(classname,'类','0'),'.','0')
    select * from tbdrop table tb
      

  2.   

    /*    结果  
    classid     classname            fatherid    scblevel             
    ----------- -------------------- ----------- -------------------- 
    1           类1                   -1          01
    2           类1.1                 1           0101
    3           类1.2                 1           0102
    4           类1.3                 1           0103
    5           类2                   -1          02
    6           类2.1                 5           0201
    7           类2.1.1               6           020101
    8           类2.1.1.1             7           02010101(8 row(s) affected)
    */----------不知道是不是lz的意思?
      

  3.   

    我想做成
    update scb set scblevel=cast((select scblever from ?????? )as varchar)+right(100+(select count(*) from scb where ???????),2)
    这样的语句
    但是问号处做不来。
      

  4.   

    其实不明白意思也可以。
    现在在下面加一行
    classid   classname fatherid  scblevel
    1          类1        -1         01
    2          类1.1      1          0101
    3          类1.2      1          0102
    4          类1.3      1          0103
    5          类2         -1        02
    6          类2.1      5          0201
    7          类2.1.1    6          020101
    8          布料       7          02010101
    9          服装       8          0201010101
    第9行属于类布料中间的小类
    那么后面的遍历数应该怎么生成?
      

  5.   

    不会吧。这个看不明白啊???????
    http://www.knowsky.com/4937.html
    上面这个网页的思路和我这个差不多的。
    只是我这里的语句没写出来而已。
    这个表的目的就是用一个表来表现一个划分类别的树形结构。
      

  6.   

    create table tempY(classid int, classname varchar(10), fatherid int, scblevel varchar(10))
    insert tempY select 1 ,'类1', -1 ,null
    union all select 2 ,'类1.1', 1,null
    union all select 3 ,'类1.2', 1, null
    union all select 4 ,'类1.3', 1, null
    union all select 5 ,'类2', -1, null
    union all select 6 ,'类2.1', 5, null
    union all select 7 ,'类2.1.1', 6, null
    union all select 8 ,'类2.1.1.1', 7 ,null
    goupdate a set scblevel=(select right('0'+ltrim(count(1)),2) from tempy where classid<=a.classid and fatherid=-1) from tempy a where fatherid=-1 and scblevel is nullwhile @@rowcount>0
    begin
    update aa set scblevel= x from tempY aa inner join (select b.classid, a.scblevel+(select right('0'+ltrim(count(1)),2) from tempY where fatherid=b.fatherid and classid<=b.classid and scblevel is null) x from tempY a,tempY b where b.fatherid=a.classid and b.scblevel is null and a.scblevel is not null) bb on aa.classid=bb.classid where aa.scblevel is null
    end
      

  7.   

    --result
    /*
    classid     classname  fatherid    scblevel   
    ----------- ---------- ----------- ---------- 
    1           类1         -1          01
    2           类1.1       1           0101
    3           类1.2       1           0102
    4           类1.3       1           0103
    5           类2         -1          02
    6           类2.1       5           0201
    7           类2.1.1     6           020101
    8           类2.1.1.1   7           02010101(所影响的行数为 8 行)
    */
      

  8.   

    把类也写进去
    create table tempY(classid int, classname varchar(10), fatherid int, scblevel varchar(10))
    insert tempY select 1 ,null, -1 ,null
    union all select 2 ,null, 1,null
    union all select 3 ,null, 1, null
    union all select 4 ,null, 1, null
    union all select 5 ,null, -1, null
    union all select 6 ,null, 5, null
    union all select 7 ,null, 6, null
    union all select 8 ,null, 7 ,nullgoupdate a set
    classname=(select '类'+ltrim(count(1)) from tempy where classid<=a.classid and fatherid=-1) ,
    scblevel=(select right('0'+ltrim(count(1)),2) from tempy where classid<=a.classid and fatherid=-1) from tempy a where fatherid=-1while @@rowcount>0
    begin
    update aa set classname= x from tempY aa inner join (select b.classid, a.classname+(select '.'+ltrim(count(1)) from tempY where fatherid=b.fatherid and classid<=b.classid and classname is null) x from tempY a,tempY b where b.fatherid=a.classid and b.classname is null and a.classname is not null) bb on aa.classid=bb.classid 
    update aa set scblevel= x from tempY aa inner join (select b.classid, a.scblevel+(select right('0'+ltrim(count(1)),2) from tempY where fatherid=b.fatherid and classid<=b.classid and scblevel is null) x from tempY a,tempY b where b.fatherid=a.classid and b.scblevel is null and a.scblevel is not null) bb on aa.classid=bb.classid
    endgoselect * from tempy