table表结构如下:
id pid name
00 null 公司
0001 00 营销部
000101 0001 A区
000102 0001 B区
000103 0001 C区
0002 00 生产部
000201 0002 物控部
000202 0002 制造部1.如何新增一个节点,如在营销部下面新增“D区”
2.删除生产部,需要把生产部下面的子节点一起删除偶是初学sql,请各位大侠帮帮忙。。另外我有一个问题,就是现在我设计是每层是按递增两位数字来算的,但如果有些情况超出范围怎么办?
如果每层都多加位数,好像有点太长了,不知道怎么设计比较好?

解决方案 »

  1.   

    2.
    if object_id('tb') is not null
    drop table tb
    create table tb(id varchar(20), pid varchar(20), DName varchar(20))
    go
    insert into tb(id,pid,DName)
    select '00',NULL, '公司' union all
    select '0001', '00', '营销部' union all
    select '000101', '0001', 'A区' union all
    select '000102', '0001', 'B区' union all
    select '000103', '0001', 'C区' union all
    select '0002', '00', '生产部' union all
    select '000201', '0002', '物控部' union all
    select '000202', '0002', '其他部' union all
    select '000203', '0002', '制造部' union all
    select '00020301', '000203', '传输部'
    go
    --select * from tb
    with tb2(id) as
    (select id from tb where DName = '生产部' 
    union all select tb.id from tb join tb2 on tb.pid like tb2.id + '%'
    )
    --select * from tb2 
    --我也新手。没明白为什么00020301会出现两次
    /*
    id
    --------------------
    0002
    000201
    000202
    000203
    00020301
    00020301(6 行受影响)
    */
    delete from tb where id in (select distinct id from tb2)
    select * from tb
    /*
    id                   pid                  DName
    -------------------- -------------------- --------------------
    00                   NULL                 公司
    0001                 00                   营销部
    000101               0001                 A区
    000102               0001                 B区
    000103               0001                 C区(5 行受影响)
    */
      

  2.   

    DECLARE @NewID varchar(100)
    DECLARE @Pid varchar(100)
    select @NewID = '0000' + cast(max((cast(a.id as int))+1) as varchar(100)),@Pid= a.pid from tb a where a.pid = (select top 1 id from tb where DName = '营销部')
    group by id,pid
    insert into tb values(right(@NewID, len(@Pid)+2), @Pid,'D区')  
    select * from tb
    /*
    id                   pid                  DName
    -------------------- -------------------- --------------------
    00                   NULL                 公司
    0001                 00                   营销部
    000101               0001                 A区
    000102               0001                 B区
    000103               0001                 C区
    0002                 00                   生产部
    000201               0002                 物控部
    000202               0002                 其他部
    000203               0002                 制造部
    00020301             000203               传输部
    000104               0001                 D区(11 行受影响)
    */
      

  3.   

    if object_id('tb') is not null
        drop table tb
    create table tb(id varchar(20), pid varchar(20), DName varchar(20))
    go
    insert into tb(id,pid,DName)
    select '00',NULL, '公司' union all
    select '0001', '00', '营销部' union all
    select '000101', '0001', 'A区' union all
    select '000102', '0001', 'B区' union all
    select '000103', '0001', 'C区' union all
    select '0002', '00', '生产部' union all
    select '000201', '0002', '物控部' union all
    select '000202', '0002', '其他部' union all
    select '000203', '0002', '制造部' union all
    select '00020301', '000203', '传输部'
    gocreate proc addnode
    @pid varchar(20), 
    @DName varchar(20),
    @len int,
    @id varchar(20) output
    as
    begin
       set @id = ''
       declare @index int
       declare @maxid int
       set @index =  power(10,@len)
       select @maxid = @index + convert(int, replace(max(id),@pid, '') + 1) from tb where pid = @pid
       select @maxid
       set @id = @pid + right(@maxid, @len)
       insert tb(id, pid, DName) values(@id, @pid, @DName)
    end
    declare @id varchar(20)
    exec addnode '0001', 'D区', 2, @id output
    select @id
      

  4.   


    2.删除生产部,需要把生产部下面的子节点一起删除delete from tb where id like '0002%'
      

  5.   

    if object_id('tb') is not null
        drop table tb
    create table tb(id varchar(20), pid varchar(20), DName varchar(20))
    go
    insert into tb(id,pid,DName)
    select '00',NULL, '公司' union all
    select '0001', '00', '营销部' union all
    select '000101', '0001', 'A区' union all
    select '000102', '0001', 'B区' union all
    select '000103', '0001', 'C区' union all
    select '0002', '00', '生产部' union all
    select '000201', '0002', '物控部' union all
    select '000202', '0002', '其他部' union all
    select '000203', '0002', '制造部' union all
    select '00020301', '000203', '传输部';with t as
    (
    select * from tb where id='00' 
    union all
    select r.* from tb r,t f where r.pid=f.id
    )
    select * from t