关于人事岗位变动(适用于bom)

--http://blog.youkuaiyun.com/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 jobinfo

select * from #jobcode

select *from #jobinfo
--思路
找出原先运维经理的一级下属,和直接上司
找出原先人事总监一级下属和直接上司

--需一中间表

select a.*,b.jobname
into #
from #jobinfo as a inner join #jobcode as b on a.jobcode=b.jobcode

with
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


如图:
company_depart deportname name jobcode senior jobname
尚瑞薪才 人事 hrjob_a hrp01 人事总监
尚瑞薪才 人事 hrjob_b hrp02 hrjob_a 人事副总监
尚瑞薪才 运维 ompjob_a omp01 运维总监
尚瑞薪才 运维 ompjob_b omp02 ompjob_b 运维副总监

优势:速度更快,存储空间小
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值