参考:
/*
标题:查询指定节点及其所有子节点的函数
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开) 
时间: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

解决方案 »

  1.   

    给你个eg
    --http://blog.csdn.net/wlzd3636
    --关于人事软件中的员工岗位调整
    --员工岗位表
    if object_id(N'jobcode','U') is not null
    drop table jobcode;
    create table jobcode
    (id  smallint identity(1,1) ,
     jobcode char(5) not null,
     jobname varchar(20) not null,
     superiorjobcode char(5) not null
    )
    --生成测试数据
    insert into jobcode(jobcode,jobname,superiorjobcode)
      select jobcode='hrp01',jobname=N'人事总监',superiorjobcode=''
      union all
      select 'hrp02',N'人事副总监','hrp01'
      union all
      select 'hrp03',N'人事经理','hrp02'
      union all
      select 'hrp04',N'idc人事','hrp03'
      union all
      select 'hrp05',N'运维人事','hrp03'
      union all
      select 'omp01',N'运维总监',''
      union all
      select 'omp02',N'运维副总监','omp01'
      union all
      select 'omp03',N'运维经理','omp02'
      union all
      select 'omp04',N'运维idc主管','omp03'
      union all
      select 'omp05',N'运维db主管','omp03'
      union all
      select 'omp06',N'运维idc','omp04'
      union all
      select 'omp07',N'运维db','omp05'
     go
    --员工信息表
    if object_id(N'jobinfo','U') is not null
    drop table jobinfo;
    create table jobinfo
    (id  int identity(1,1),
     name varchar(20) not null,
     jobcode char(5) not null,
     senior varchar(20) not null
    );
    --生成测试数据
    insert into jobinfo(name,jobcode,senior)
      select name='hrjob_a', jobcode='hrp01',senior=''
      union all
      select 'hrjob_b','hrp02','hrjob_a'
      union all
      select 'hrjob_c','hrp03','hrjob_b'
      union all
      select 'hrjob_d','hrp04','hrjob_c'
      union all
      select 'hrjob_e','hrp05','hrjob_c'
      union all
      select 'ompjob_a','omp01',''
      union all
      select 'ompjob_b','omp02','ompjob_a'
      union all
      select 'ompjob_c','omp03','ompjob_b'
      union all
      select 'ompjob_d','omp04','ompjob_c'
      union all
      select 'ompjob_e','omp05','ompjob_c'
      union all
      select 'ompjob_f','omp06','ompjob_d'
      union all
      select 'ompjob_h','omp07','ompjob_e'
     go--常用统计
    返回每个员工下面的下级员工
    --2005
    if object_id(N'bom_job','IF') is not null
    drop function bom_job;
    create function bom_job(@name varchar(20)) returns table
    as
    return
    with xwj
    as
    (select a.id,a.name,a.jobcode,a.senior,b.jobname
      from jobinfo as a
      inner join jobcode as b 
      on a.jobcode=b.jobcode
    ),
    xwj2
    as
    (select id,name,jobcode,senior,@name as maxsenior,jobname from xwj where name=@name
     union all 
     select b.id,a.name,a.jobcode,b.name as senior,maxsenior=@name,a.jobname from xwj as a inner join xwj2 as b on b.name=a.senior
    )
    select * from xwj2
    go
    --演示
    select b.* 
     from jobinfo as a 
     cross apply
     bom_job(a.name) as b
     where a.name='hrjob_b'
    /*
    id   name   jobcode senior      maxsenior         jobname
    ----------- -------------------- ------- -------------------- 
    2   hrjob_b  hrp02   hrjob_a   hrjob_b            人事副总监
    2   hrjob_c  hrp03   hrjob_b   hrjob_b              人事经理
    2   hrjob_d  hrp04   hrjob_c   hrjob_b              idc人事
    2   hrjob_e  hrp05   hrjob_c   hrjob_b              运维人事(4 行受影响)
    */
    一般在薪筹软件里员工变动很大比如
    人事总监hrjob_a 换岗到运维部当 运维经理
    而运维经理ompjob_c 换岗到人事部门当人事总监则
    原表里面的数据以及下属员工的上级数据都得变动
    为了演示
    从新生成了一个临时表
    --生成数据
    select * into #jobcode
     from jobcode
    select * into #jobinfo
     from jobinfoselect * from #jobcodeselect *from #jobinfo
    --思路
    找出原先运维经理的一级下属,和直接上司
    找出原先人事总监一级下属和直接上司--需一中间表select a.*,b.jobname
    into # 
    from #jobinfo as a  inner join #jobcode as b on a.jobcode=b.jobcodewith 
    xwj
    as
    (
    select name,jobcode,senior,jobname,id=0 from # where name='ompjob_c'
    union all
    select a.name,a.jobcode,a.senior,a.jobname,b.id+1 from  # as a inner join xwj as b on b.name=a.senior
     )
    ,
    xwj2
    as
    (select name,jobcode,senior,jobname,id=0 from # where name='ompjob_c'
     union all
     select a.name,a.jobcode,a.senior,a.jobname,b.id-1 from  # as a inner join xwj2 as b on a.name=b.senior
    )
    ,
    xwj3
    as
    (select * from xwj
     union
     select * from xwj2
    )
    select * from xwj3 where id=1 or id=-1--分别update
    建议:
    --2005 xml
    用xml 来存储一个部门或组的员工信息
    declare @xml xml
    select @xml=
    '<company company_depart="尚瑞薪才">
      <deport deportname="人事">
        <job name="hrjob_a" jobcode="hrp01" senior="" jobname="人事总监"/>
      </deport>
      <deport deportname="运维">
        <job name="ompjob_a" jobcode="omp01" senior="" jobname="运维总监"/>
      </deport>
    </company>'
    select @xml eg:create table company_xml
    (id smallint identity(1,1),
     part xml not null
    )insert into company_xml(part)
    values(
    cast('<company company_depart="尚瑞薪才">
      <deport deportname="人事">
        <job name="hrjob_a" jobcode="hrp01" senior="" jobname="人事总监"/>
        <job name="hrjob_b" jobcode="hrp02" senior="hrjob_a" jobname="人事副总监"/>
      </deport>
    </company>' as xml)
    )insert into company_xml(part)
    values(
    cast('<company company_depart="尚瑞薪才">
      <deport deportname="运维">
        <job name="ompjob_a" jobcode="omp01" senior="" jobname="运维总监"/>
        <job name="ompjob_b" jobcode="omp02" senior="ompjob_b" jobname="运维副总监"/>
      </deport>
    </company>' as xml)
    )
    select  company_depart,deportname,name,jobcode,senior,jobname from company_xml  as a
    cross apply
    (
    select  t.c.value('@company_depart','sysname') as company_depart,
            t.c.value('(deport/@deportname)[1]','sysname') as deportname
         from a.part.nodes('/company') as t(c)
    ) as b
    cross apply
    ( select * from
       (select id,b.name,b.jobcode,b.senior,b.jobname
          from
            (select id,part.query('/company/deport/job') as job
              from  company_xml) as a
             cross apply
             (select   
                  t.c.value('@name','sysname') as name,
                  t.c.value('@jobcode','sysname') as jobcode,
                  t.c.value('@senior','sysname') as senior,
                  t.c.value('@jobname','sysname') as jobname
                 from a.job.nodes('/job')  as t(c)
              )as b ) as c 
       where a.id=c.id
    )as d如图:
     
    优势:速度更快,存储空间小.