有一张部门表结构如下:
部门id   部门名称          父部门id      系统类型
1 总部   0             aaa2 客户部   1             bbb3 人事部   1             4 财务部   1             bbb5 客户关系   2              6 客户攻关   2             ddd7  会计            4   8  出纳   4              eee 9  收银   4

此表为一组织结构表,现在我希望将表中所属系统这个字段内容为空的填上数据,规则如下:
若某个部门的系统类型为空,则将该部门的所属系统设置为该部门的上级部门的系统类型;若不为空,则保留            

解决方案 »

  1.   

    update a
    set      a.系统类型=b.系统类型
    from  tabA a,tabA b
    where a.父部门id=b.部门id and a.系统类型 is null
      

  2.   

    update A
    set A.系统类型=B.系统类型
    from table1 A left join table1 B ON A.父部门ID=B.部门ID
    where A.系统类型 is null
      

  3.   

    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([部门id] int,[部门名称] varchar(8),[父部门id] int,[系统类型] varchar(3))
    insert [tb]
    select 1,'总部',0,'aaa' union all
    select 2,'客户部',1,'bbb' union all
    select 3,'人事部',1,null union all
    select 4,'财务部',1,'bbb' union all
    select 5,'客户关系',2,null union all
    select 6,'客户攻关',2,'ddd' union all
    select 7,'会计',4,null union all
    select 8,'出纳',4,'eee' union all
    select 9,'收银',4,nullupdate a set [系统类型] =(select top 1 b.[系统类型] from [tb] b where a.[父部门id]=b.[父部门id] and b.[系统类型] is not null)
    from [tb] aselect * from tb
    /*
    部门id        部门名称     父部门id       系统类型 
    ----------- -------- ----------- ---- 
    1           总部       0           aaa
    2           客户部      1           bbb
    3           人事部      1           bbb
    4           财务部      1           bbb
    5           客户关系     2           ddd
    6           客户攻关     2           ddd
    7           会计       4           eee
    8           出纳       4           eee
    9           收银       4           eee(所影响的行数为 9 行)
    */
      

  4.   

    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([部门id] int,[部门名称] varchar(8),[父部门id] int,[系统类型] varchar(3))
    insert [tb]
    select 1,'总部',0,'aaa' union all
    select 2,'客户部',1,'bbb' union all
    select 3,'人事部',1,null union all
    select 4,'财务部',1,'bbb' union all
    select 5,'客户关系',2,null union all
    select 6,'客户攻关',2,'ddd' union all
    select 7,'会计',4,null union all
    select 8,'出纳',4,'eee' union all
    select 9,'收银',4,nullupdate a set a.[系统类型] = b.[系统类型] from [tb] a,[tb] b where a.[父部门id]=b.[父部门id] and b.[系统类型] is not null
    select * from tb
    /*
    部门id        部门名称     父部门id       系统类型 
    ----------- -------- ----------- ---- 
    1           总部       0           aaa
    2           客户部      1           bbb
    3           人事部      1           bbb
    4           财务部      1           bbb
    5           客户关系     2           ddd
    6           客户攻关     2           ddd
    7           会计       4           eee
    8           出纳       4           eee
    9           收银       4           eee(所影响的行数为 9 行)
    */
      

  5.   


    if OBJECT_ID('tb') is not null
    drop table tb
    go
    create table tb (部门id int,部门名称 varchar(10),父部门id int ,系统类型 varchar(6))
    insert into tb
    select 1, '总部', 0, 'aaa' union allselect 2, '客户部', 1, 'bbb' union allselect 3, '人事部', 1, null   union allselect 4, '财务部', 1, 'bbb' union allselect 5, '客户关系', 2,null    union allselect 6, '客户攻关', 2, 'ddd' union allselect 7, '会计', 4,  null union allselect 8, '出纳', 4, 'eee'   union allselect 9, '收银', 4,nullselect * from tb部门id 部门名称 父部门id 系统类型
    1 总部 0 aaa
    2 客户部 1 bbb
    3 人事部 1 NULL
    4 财务部 1 bbb
    5 客户关系 2 NULL
    6 客户攻关 2 ddd
    7 会计 4 NULL
    8 出纳 4 eee
    9 收银 4 NULLupdate a  set a.系统类型= case when a.系统类型 is null then b.系统类型 
                              else a.系统类型 end 
    from tb a,tb b  where a.父部门id=b.部门idselect * from tb部门id 部门名称 父部门id 系统类型
    1 总部 0 aaa
    2 客户部 1 bbb
    3 人事部 1 aaa
    4 财务部 1 bbb
    5 客户关系 2 bbb
    6 客户攻关 2 ddd
    7 会计 4 bbb
    8 出纳 4 eee
    9 收银 4 bbb