日期:2014-05-17 浏览次数:20567 次
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