数据表很简单,就是4个值 id(自增字段),pid, title,userid由id和pid来确定父子关系,如果pid的值等于id,那么这个pid就是id的子目录
现在是这样的,更新表必须是某个userid的所有值全部删除然后在insert进去新的  新的为xml 格式 
就是按照父子关系的一个xml表 如:
<folder  title= A>
---<folder title=B/>
---<folder title=C/>
---<folder title=D>
------<folder title=E/>
------<folder title=F/>
---</folder >
</folder>如果是update的话还好办,因为update的话id和pid不会变,但现在是必须删除了再添加,这样id肯定就变了,要保证一致的父子关系,所以pid也要变,咋个能一起把id和pid一起动态更新呢?

解决方案 »

  1.   

    照着写create table #EnterPrise
    (
      Department nvarchar(50),--部门名称
      ParentDept nvarchar(50),--上级部门
      DepartManage nvarchar(30)--部门经理
    )
    insert into #EnterPrise select '技术部','总经办','Tom'
    insert into #EnterPrise select '商务部','总经办','Jeffry'
    insert into #EnterPrise select '商务一部','商务部','ViVi'
    insert into #EnterPrise select '商务二部','商务部','Peter'
    insert into #EnterPrise select '程序组','技术部','GiGi'
    insert into #EnterPrise select '设计组','技术部','yoyo'
    insert into #EnterPrise select '专项组','程序组','Yue'
    insert into #EnterPrise select '总经办','','Boss'
    --查询部门经理是Tom的下面的部门名称
    ;with hgo as
    (
       select *,0 as rank from #EnterPrise where DepartManage='Tom'
       union all
       select h.*,h1.rank+1 from #EnterPrise h join hgo h1 on h.ParentDept=h1.Department
    )
    select * from hgo
    /*
    Department           ParentDept                DepartManage      rank
    --------------- -------------------- ----------------------- -----------
    技术部               总经办                    Tom               0
    程序组               技术部                    GiGi              1
    设计组               技术部                    yoyo              1
    专项组               程序组                    Yue               2
    */
    --查询部门经理是GiGi的上级部门名称
    ;with hgo as
    (
       select *,0 as rank from #EnterPrise where DepartManage='GiGi'
       union all
       select h.*,h1.rank+1 from #EnterPrise h join hgo h1 on h.Department=h1.ParentDept
    )
    select * from hgo
    /*
    Department               ParentDept          DepartManage    rank
    -------------------- ----------------------  -----------  -----------
    程序组                   技术部                 GiGi           0
    技术部                   总经办                 Tom            1
    总经办                                          Boss           2
    */本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ws_hgo/archive/2010/01/31/5274571.aspx
      

  2.   

    我想知道怎么取得所有的父目录的 新id,然后在放入子目录的pid中
      

  3.   

    比如 上面例子的<folder title= A> 这个的id ,取得这个id能写到B,C,D的pid里面;D的id 能写到E,F的pid里面,这样才不会打乱父子关系
      

  4.   

    比如给我一个xml表
    <folder title= A>
    ---<folder title=B/>
    ---<folder title=C/>
    ---<folder title=D>
    ------<folder title=E/>
    ------<folder title=F/>
    ---</folder >
    </folder>
    就这样的,我要把这种父子关系写到sql里面去,就是通过id和pid来的。因为是insert,所以必须要判断出父目录的id号,当然这个父目录也有可能是其他的子目录