zoukankan      html  css  js  c++  java
  • 存储过程学习(004)--象写程序一样的写存储过程及调试

    业务描述:从HR系统中同步人员信息(新入职,离职)

    /*
    同步HR系统新入职及离职员工数据
    新入职:插入到对应部门的未分配部门里,事业部需区分文职,非文职 并插入信息到OA的员工表
    处理过的用户标记为2
    
    数据量不要超过1000条,否则有可能报错^_^.
    
    数据源:tb_oa_employee(HR)
    目标:OA_TAB_STAFF,OA_TAB_GWSTAFFS
    */
    
    ALTER PROCEDURE dbo.HrUserDataToOa
    AS
    declare @isadd int
    declare @hrid int
    declare @staffno varchar(90)
    declare @staffname varchar(90)
    declare @wz int
    declare @deptcode varchar(90)
    declare @deptname varchar(90)
    declare @deptchildname varchar(90)
    declare @jobname varchar(90)
    declare @idcard varchar(90)
    declare @birth varchar(90)
    declare @sex varchar(1)
    declare @datejoin datetime
    declare @postid int
    declare yb cursor for 
    select C_DATASTATUS,C_OID,C_CODE,C_NAME,C_ZWSX=(case C_ZWSX when '文职' then 1 when '非文职' then 0 end),
    C_BUMENCODE,C_BUMEN,C_ORGNAME,C_JOBNAME,C_IDCARD,C_BIRTH,
    C_SEX=(case C_SEX when '' then 'M' when '' then 'F' end),C_HIREDATE 
    from [192.168.0.170].ehr.dbo.tb_oa_employee 
    where c_linestatus=1 and c_zwsx is not null 
    
    open yb
    fetch next from yb into @isadd,@hrid,@staffno,@staffname,@wz,@deptcode,@deptname,@deptchildname,@jobname,@idcard,@birth,@sex,@datejoin
    while @@fetch_status=0
    begin
        if @isadd = 1 /*新入职*/
            begin
                /*获取对应的OA职位编码 通过文职*/
                /*select @postid=c_oajob from [192.168.0.170].ehr.dbo.tb_oa_job where c_code =@deptcode and c_wenzhi=@wz*/
                set @postid = (select c_oajob from [192.168.0.170].ehr.dbo.tb_oa_job where c_code =@deptcode and c_wenzhi=@wz)
                if @postid is not null and @postid !='' and exists(select * from oa_tab_post where oa_post_id = @postid)
                begin
                    /*print @staffno+'部门:'+@deptname+'deptcode'+@deptcode+'wz'+cast(@wz as varchar(10))+'postid:'+ cast(@postid as varchar(20))*/
                    /*如果在职位表中不存在,将该人员插入到未非配职位上*/
                    if not exists(select * from OA_TAB_STAFF where oa_staf_code=@staffno) 
                    begin
                        insert into OA_TAB_STAFF(OA_STAF_CODE,OA_POST_ID,OA_IS_MAIN_POST) values(@staffno,@postid,1)
                    end
                    /*如果没有人员基础信息,则插入人员基本信息*/
                    if not exists(select * from OA_TAB_GWSTAFFS where oa_gws_staffno=@staffno)
                    begin
                        INSERT INTO OA_TAB_GWSTAFFS(OA_GWS_STAFFNO,OA_GWS_HRNAME,OA_GWS_HRSOCIETYID,OA_GWS_HRBIRTHDAY,OA_GWS_HRSEX,OA_GWS_HRDEPT1,OA_GWS_HRDEPT2,OA_GWS_HRDEPT3,OA_GWS_HRJOINDATE,OA_GWS_HRPOSITION,OA_GWS_HRLIVE)
                         values(@staffno,@staffname,@idcard,@birth,@sex,@deptname,@deptchildname,'',@datejoin,@jobname,1)
                    end 
                    update [192.168.0.170].ehr.dbo.tb_oa_employee set c_linestatus=3 where C_OID = @hrid
                    
                end
            end
        else
            begin
                /*修改为离职状态*/
                UPDATE OA_TAB_GWSTAFFS SET OA_GWS_HRLIVE=0  where OA_GWS_STAFFNO = @staffno
                /*删除职责信息*/
                delete from OA_TAB_STAFF_DUTY  where oa_stafpost_id in
                (select oa_stafpost_id from oa_tab_staff where oa_staf_code=@staffno)
                /*删除岗位信息*/
                delete from OA_TAB_STAFF where OA_STAF_CODE = @staffno
                /*修改HR数据为已处理状态*/
                update [192.168.0.170].ehr.dbo.tb_oa_employee set c_linestatus=2 where C_OID = @hrid
            end
            
        fetch next from yb into @isadd,@hrid,@staffno,@staffname,@wz,@deptcode,@deptname,@deptchildname,@jobname,@idcard,@birth,@sex,@datejoin
    end
    close yb
    deallocate yb


    碰到的问题:循环时发现,所有的记录状态都变为2了,也就是都被处理过了。可实际上有些记录的@postid is null,应该不会执行才对。找了一下原来是select赋值的问题,当为空时,select 赋值会保留上一次的数据。

    参考:http://www.cnblogs.com/McJeremy/archive/2011/03/30/1999508.html(set和select的区别)第三条

  • 相关阅读:
    day5
    \_\_setitem\_\_和\_\_getitem和\_\_delitem__
    描述符(\_\_get\_\_和\_\_set\_\_和\_\_delete\_\_)
    \_\_getattribute\_\_
    面向对象进阶小结
    property装饰器
    super()方法详解
    菱形继承问题
    类的派生
    类的继承
  • 原文地址:https://www.cnblogs.com/sportdog/p/3593288.html
Copyright © 2011-2022 走看看