一数据库操作题,帮帮看看怎么写,
H_Code号码表存放员工工号,emp员工信息表。由于设计问题出现部分员工工号重复现象,现在需要对工号重复的员工进行重新编号。
员工工号编号规则每次从H_Code表中取最大值加1。emp表中e_id为主键自增长,e_code为员工工号。
请写出处理重号问题的完整存储过程。

解决方案 »

  1.   


    ----1.先把重复的记录用这个查询出来
    ----2.把这个查询出来的值做update,使用序列(seqName.nextVal)做为你的e_code
    ----3.注意,一定要用rowid做为修改记录的条件
    select a.*,a.rowid from emp a where a.rowid!=(select max(rowid) from emp b where a.e_code=b.e_code);---查询重复记录
      

  2.   

    参考sql:
    create or replace sp_upd_emp 
    is
       v_code number;
       v_id emp%e_id%type;
       cursor cur_emp is
       select e_id from emp e 
       where e_code in (select e_code from emp em group by e_code having count(*)>1);
    begin
       select max(H_Code) into  v_code from  H_Code;
       if v_code is null then
          v_code:=1;
       else
          v_code:=v_code+1;
       end if;
       open cur_emp;
       loop
          fetch cur_emp into v_id;
          exit when cur_emp%notfound;
          update emp set e_code=v_code where e_id=v_id;
          v_code:=v_code+1;
       end loop; 
       commit;
    end;