關于值班管理的存儲過程
2024-07-21 02:05:42
供稿:網友
一個bt主任的要求 值班管理 要求如下
1 一組隊列 n 個人, 有4種角色,領導,漢子,大媽,司機。n個人根據自己角色按順序排好隊
2 值班要求:周一到周日 1個領導值班1個司機值班;周一到周日 每晚1個漢子 值班;周六 周日 上午下午2個大媽值班;假期每天1個領導1個司機1個漢子上午下午2個大媽
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 @[email protected]+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 [email protected] 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 @[email protected]+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 [email protected] 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 @[email protected]+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 [email protected] where [datetime]=(dateadd(day,@i,{fn curdate()})) if (@@error !=0) begin rollback tran set @msg='error3' return end set @[email protected]+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 [email protected] 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 [email protected] where [datetime]=(dateadd(day,@i,{fn curdate()})) if (@@error !=0) begin --rollback tran set @msg='error3' return end
end set @[email protected]+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
以上為隊列改變時生成新值班安排的存儲過程
其他 諸如 規定假期 調整人員 大同小異 歡迎批評指正