####################
最高学历更新
本匿名块实现的功能:
需要升级表为tb_sta_degree。 这个存的是员工教育经历。需要做的事情是给每个员工的最高学历进行标记,即将c_highest_education置1,注意每个员工有多条教育经历,仅最高学历那条置1.具体实现方法建议下:
1. 便利tb_sta_degree该表,以人为单位。当该人下面有c_education_level=0(研究生)的情形,该行c_highest_education的置1.
2. 没有0情形,有1情形的数据,该行c_highest_education的置1
3.没有1情形,有2情形的,该行c_highest_education的置1
以此类推,最大数字为7(小学)
总之一句话,就是一个人有多条学历,仅将这个人的最高学历的c_highest_education字段置为1.。
最高学历的判断方法就是找到这个人c_education_level值最小的那行记录,然后将该行的c_highest_education置为1。如果该行的c_highest_education原来有值,就保留c_highest_education的值,不要更新它。
####################
declare
cursor c1
is
--查看每个人c_education_level的最小值
select
c_employee_id,
min(c_education_level) min_level
from
tb_sta_degree group by c_employee_id ;
v_c_employee_id tb_sta_degree.c_employee_id%type;
v_c_education_min_level tb_sta_degree.c_education_level%type;
v_highest_education_count number(30);
begin
for k in c1 loop
v_c_employee_id:=k.c_employee_id;
v_c_education_min_level:=k.min_level;
--判断c_highest_education原来是否优有值
select count(c_highest_education) into v_highest_education_count from tb_sta_degree where c_employee_id=v_c_employee_id;
if v_highest_education_count =0
then
update tb_sta_degree
set
c_highest_education=1 ,c_highest_degree=1,C_major_or_not=1
where c_employee_id=v_c_employee_id and c_education_level=v_c_education_min_level;
commit;
--dbms_output.put_line('c_employee_id:'|| v_c_employee_id||' c_education_min_level:'||v_c_education_min_level);
end if;
end loop;
end;
/
验证结果:
select c_employee_id,C_UNIVERSITY_COLLEGE,C_MAJOR,c_education_level,c_degree,c_highest_education, c_highest_degree,C_major_or_not from tb_sta_degree
where c_employee_id='77ab4adeab7d4ec98370d563be40c7a6'
--验证每个员工c_degree是否有重复值
select c_employee_id,count(c_degree) from tb_sta_degree where c_highest_education!=1 group by c_employee_id having count(c_degree) > 1
########
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/28916011/viewspace-2108400/ ,如需转载,请注明出处,否则将追究法律责任。