日期:2014-05-17  浏览次数:20518 次

存储过程怎么实现?
8月1日0:00-8:00是丙班,8:00-16:00是丁,16:00-24:00是甲
8月2日0:00-8:00是乙班,8:00-16:00是丁,16:00-24:00是甲
8月3日0:00-8:00是乙班,8:00-16:00是丙,16:00-24:00是丁
8月4日0:00-8:00是甲班,8:00-16:00是丙,16:00-24:00是丁
8月5日0:00-8:00是甲班,8:00-16:00是乙,16:00-24:00是丙
8月6日0:00-8:00是丁班,8:00-16:00是乙,16:00-24:00是丙
8月7日0:00-8:00是丁班,8:00-16:00是甲,16:00-24:00是乙
8月8日0:00-8:00是丙班,8:00-16:00是甲,16:00-24:00是乙

8月9日0:00-8:00是丙班,8:00-16:00是丁,16:00-24:00是甲
8月10日0:00-8:00是乙班,8:00-16:00是丁,16:00-24:00是甲
8月11日0:00-8:00是乙班,8:00-16:00是丙,16:00-24:00是丁
8月12日0:00-8:00是甲班,8:00-16:00是丙,16:00-24:00是丁
8月13日0:00-8:00是甲班,8:00-16:00是乙,16:00-24:00是丙
8月14日0:00-8:00是丁班,8:00-16:00是乙,16:00-24:00是丙
8月15日0:00-8:00是丁班,8:00-16:00是甲,16:00-24:00是乙
8月16日0:00-8:00是丙班,8:00-16:00是甲,16:00-24:00是乙

我想像上边这样做出来,实现自动生成每个时间段的班号,用sql存储过程实现,请问怎么实现,最好给出代码!谢谢!!

------解决方案--------------------
create table tb(id int,class varchar(5))
create table tc(class_date datetime,class1 varchar(5),class2 varchar(5),class3 varchar(5))

insert into tb(id,class) values(1,'甲')
insert into tb(id,class) values(2,'丁')
insert into tb(id,class) values(3,'丙')
insert into tb(id,class) values(4,'乙')


declare @start_date datetime,@i int,@j int
set @start_date='2013-08-01'
select @i=0,@j=0

while @i<30
begin
select @j=case when @j+1=5 then 1 else @j+1 end
insert into tc(class_date,class1,class2,class3)
select dateadd(day,@i,@start_date),(select class from tb b where b.id=case when a.id+2>4 then 4-a.id+2 else a.id+2 end)
,(select class from tb c where c.id=case when a.id+1>4 then 4-a.id+2 else a.id+1 end),class
from tb a
where a.id=@j
set @i=@i+1

insert into tc(class_date,class1,class2,class3)
select dateadd(day,@i,@start_date),(select class from tb b where b.id=case when a.id+3>4 then 4-a.id+1 else a.id+3 end)
,(select class from tb c where c.id=case when a.id+1>4 then 4-a.id+1 else a.id+1 end),class