• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

关于值班管理的存储过程_sqlserver

sqlserver 搞代码 7年前 (2018-06-16) 176次浏览 已收录 0个评论

一个bt主任的要求 值班管理  要求如下

1 一组队列  n 个人, 有4种角色,领导,汉子,大妈,司机。n个人根据自己角色按顺序排好队

2 值班要求:周一到周日 1个领导值班1个司机值班;周一到周日 每晚1个汉子 值班;周六 周日 上午下午2个大妈值班;假期每天1个领导1个司机1个汉子上午下午2个大妈

http://www.gaodaima.com/34517.html关于值班管理的存储过程_sqlserver

3 要求队列可增删查该 ,人员顺序可以调整,队列发生变化时,值班表自动更新

4 要求队列人员随时可以抽调对列中的人员不参加本轮排序(出差或请假)下轮继续按队列顺序排序,人员抽调后 ,队列自动向前顶替

5 换班等…

建2个表

1 watching

    [datetime] 日期  [weekday]星期 [leaderid]领导id [maleid]汉子id [female1]大妈1id [female2]大妈2id  [driverid] 司机id  [mark]备注

2 watching_person

   [ordercode]人员编号 [personid] 人员id  [part]人员角色 [leave]是否离开  [mark]备注

part 为人员角色 1领导2汉子3大妈4司机

当新的队列产生时需要更细从明天以后的值班安排表(此处为30天),然后将 按角色排队好的 起始位置传给存储过程 (即 领到从第几位开始排 司机从第几位开始排 汉子 大妈…)

create proc Proc_WatchingSetup   –参数为四种角色的起始位置  @leader int,  @Male int,  @Female int,  @Driver intas

declare @i int                                           –计数器declare @j intdeclare @PersonID intdeclare @weekday intdeclare @InsertPoint datetimedeclare @msg char(20)

set @i=1set @j=1

— 事务开始Begin tran ReChange–删除明天以后的记录(队列已改变删除以前的)delete from Watching where [Datetime]>GetDate()

if (@@error <>0)    Begin      rollback tran     set @msg=’error1′     return   end

–重新插入后30天的日期和星期while @i<=30  begin     insert Watching (Datetime,WeekDay) values (dateadd(day,@i,{fn curdate()}),datepart(weekday,dateadd(day,@i,{fn curdate()})))          set @i=@i+1  end

if (@@error !=0)    Begin      rollback tran     set @msg=’error2′     return   end

–开始使用游标

set @j=1

–////首先按排队顺序读出领导的队列

declare cur_watchingPerson scroll cursor For   select Personid  from watching_person where part=1 order by orderCode asc

open cur_watchingPerson

–移动到开始位置fetch absolute @leader from cur_watchingPerson into @PersonIDif @@fetch_status=-1   fetch first from cur_watchingperson into @PersonID

set @i=1

while @i<=30  begin    

     while @j<=7 –最长可能是1人插入7天       begin                update watching set LeaderId=@PersonID where [datetime]=(dateadd(day,@i,{fn curdate()}))                if (@@error !=0)           Begin            rollback tran           set @msg=’error3′          return        end        –如果不足7天就到周末 退出循环 换人        select @weekday=datepart(weekday,dateadd(day,@i,{fn curdate()}))        set @i=@i+1        if (@weekday=1)          break                 end

     set @j=1          fetch next from cur_watchingperson into @PersonID     — 如果超出边界 回头队列第一位     if @@fetch_status=-1        fetch first from cur_watchingperson into @PersonID  end

Close cur_watchingPersondeallocate cur_watchingPerson

–////////////司机很领导完全一样declare cur_watchingPerson4 scroll cursor For   select Personid  from watching_person where part=4 order by orderCode asc

open cur_watchingPerson4

–移动到开始位置fetch absolute @driver from cur_watchingPerson4 into @PersonIDif @@fetch_status=-1    fetch first from cur_watchingperson4 into @PersonID

set @i=1

while @i<=30  begin     while @j<=7 –最长可能是1人插入7天       begin                update watching set driverId=@PersonID where [datetime]=(dateadd(day,@i,{fn curdate()}))                if (@@error !=0)           Begin           –rollback tran          set @msg=’error3′          return        end

        select @weekday=datepart(weekday,dateadd(day,@i,{fn curdate()}))        set @i=@i+1        if (@weekday=1)          break                 end

     set @j=1     fetch next from cur_watchingperson4 into @PersonID     — 如果超出边界 回头队列第一位     if @@fetch_status=-1        fetch first from cur_watchingperson4 into @PersonID  end

Close cur_watchingPerson4deallocate cur_watchingPerson4

–///////////

–汉子每天1人值夜班 相对容易declare cur_watchingPerson2 scroll cursor For   select Personid  from watching_person where part=2 order by orderCode asc

open cur_watchingPerson2

–移动到开始位置fetch absolute @male from cur_watchingPerson2 into @PersonIDif @@fetch_status=-1    fetch first from cur_watchingperson2 into @PersonID

set @i=1

while @i<=30  begin      update watching set MaleId=@PersonID where [datetime]=(dateadd(day,@i,{fn curdate()}))           if (@@error !=0)        Begin           rollback tran          set @msg=’error3′          return       end             set @i=@i+1

     fetch next from cur_watchingperson2 into @PersonID     — 如果超出边界 回头队列第一位     if @@fetch_status=-1            fetch first from cur_watchingperson2 into @PersonID   end

Close cur_watchingPerson2deallocate cur_watchingPerson2

–大妈每周六周日2人值白班declare cur_watchingPerson3 scroll cursor For   select Personid  from watching_person where part=3 order by orderCode asc

open cur_watchingPerson3

fetch absolute @female from cur_watchingPerson3 into @PersonIDif @@fetch_status=-1    fetch first from cur_watchingperson3 into @PersonID

set @i=1

while @i<=30  begin

     select @weekday=[weekday] from watching where [datetime]=(dateadd(day,@i,{fn curdate()}))           –判断 只有周末的半天才值班 安排2人     if @weekday=7 or @weekday=1        begin                       –插入第一位        update watching set Female1=@PersonID where [datetime]=(dateadd(day,@i,{fn curdate()}))                     if (@@error !=0)          Begin            rollback tran           set @msg=’error3′          return        end

        fetch next from cur_watchingperson3 into @PersonID        — 如果超出边界 回头队列第一位        if @@fetch_status=-1               fetch first from cur_watchingperson3 into @PersonID                –插入第二位        update watching set Female2=@PersonID where [datetime]=(dateadd(day,@i,{fn curdate()}))                     if (@@error !=0)          Begin           –rollback tran           set @msg=’error3′          return        end

       end             set @i=@i+1      fetch next from cur_watchingperson3 into @PersonID        — 如果超出边界 回头队列第一位        if @@fetch_status=-1               fetch first from cur_watchingperson3 into @PersonID        end

Close cur_watchingPerson3deallocate cur_watchingPerson3

commit tran

以上为队列改变时生成新值班安排的存储过程

其他 诸如 规定假期 调整人员 大同小异 欢迎批评指正

欢迎大家阅读《关于值班管理的存储过程_sqlserver,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:关于值班管理的存储过程_sqlserver

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址