读书人

存储过程如何实现

发布时间: 2013-09-06 10:17:17 作者: rapoo

存储过程怎么实现?
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


from tb a
where a.id=@j
set @i=@i+1
end

select convert(varchar(10),class_date,120) as class_date
,'0:00-8:00 是'+class1+'班' as class1
,'8:00-16:00 是'+class2 as class2
,'16:00-24:00是'+class3 as class3
from tc

drop table tb,tc


/*
class_dateclass1class2class3
------------------------------
2013-08-010:00-8:00 是丙班8:00-16:00 是丁16:00-24:00是甲
2013-08-020:00-8:00 是乙班8:00-16:00 是丁16:00-24:00是甲
2013-08-030:00-8:00 是乙班8:00-16:00 是丙16:00-24:00是丁
2013-08-040:00-8:00 是丙班8:00-16:00 是丙16:00-24:00是丁
2013-08-050:00-8:00 是丙班8:00-16:00 是乙16:00-24:00是丙
2013-08-060:00-8:00 是丁班8:00-16:00 是乙16:00-24:00是丙
2013-08-070:00-8:00 是丁班8:00-16:00 是丁16:00-24:00是乙
2013-08-080:00-8:00 是甲班8:00-16:00 是甲16:00-24:00是乙
2013-08-090:00-8:00 是丙班8:00-16:00 是丁16:00-24:00是甲
2013-08-100:00-8:00 是乙班8:00-16:00 是丁16:00-24:00是甲
2013-08-110:00-8:00 是乙班8:00-16:00 是丙16:00-24:00是丁
2013-08-120:00-8:00 是丙班8:00-16:00 是丙16:00-24:00是丁
2013-08-130:00-8:00 是丙班8:00-16:00 是乙16:00-24:00是丙
2013-08-140:00-8:00 是丁班8:00-16:00 是乙16:00-24:00是丙
2013-08-150:00-8:00 是丁班8:00-16:00 是丁16:00-24:00是乙
2013-08-160:00-8:00 是甲班8:00-16:00 是甲16:00-24:00是乙
2013-08-170:00-8:00 是丙班8:00-16:00 是丁16:00-24:00是甲
2013-08-180:00-8:00 是乙班8:00-16:00 是丁16:00-24:00是甲
2013-08-190:00-8:00 是乙班8:00-16:00 是丙16:00-24:00是丁
2013-08-200:00-8:00 是丙班8:00-16:00 是丙16:00-24:00是丁
2013-08-210:00-8:00 是丙班8:00-16:00 是乙16:00-24:00是丙
2013-08-220:00-8:00 是丁班8:00-16:00 是乙16:00-24:00是丙
2013-08-230:00-8:00 是丁班8:00-16:00 是丁16:00-24:00是乙
2013-08-240:00-8:00 是甲班8:00-16:00 是甲16:00-24:00是乙
2013-08-250:00-8:00 是丙班8:00-16:00 是丁16:00-24:00是甲
2013-08-260:00-8:00 是乙班8:00-16:00 是丁16:00-24:00是甲
2013-08-270:00-8:00 是乙班8:00-16:00 是丙16:00-24:00是丁
2013-08-280:00-8:00 是丙班8:00-16:00 是丙16:00-24:00是丁
2013-08-290:00-8:00 是丙班8:00-16:00 是乙16:00-24:00是丙
2013-08-300:00-8:00 是丁班8:00-16:00 是乙16:00-24:00是丙
*/

读书人网 >SQL Server

热点推荐