获取年份的day,然后插入到数据库declare @day datetime set @day='' set @day=day('2008.01')不知道LZ是否是这个意思
联合年月建表,正好这里有个例子,说的是先查出最小日期和最大日期。 你的是2008.02-2010.03,然后按月建表,一共22张。接下去你想干嘛干嘛... begin TRANSACTIONdeclare @error intdeclare @maxt varchar(6) set @maxt=(select max(distinct CONVERT(varchar(6),ddate,112)) from datasaldayshpplu_tmp )declare @mint varchar(6) set @mint=(select min(distinct CONVERT(varchar(6),ddate,112)) from datasaldayshpplu_tmp )declare @t varchar(6) set @t=@mintdeclare @sqlstr varchar(500)while @t<=@maxt beginif right(@t,2)<='12' begin set @sqlstr='if object_id('+''''+'DataSalDayShpPlu_'+right(@t,4)+''''+') is not null drop table DataSalDayShpPlu_'+right(@t,4) +' select * into [DataSalDayShpPlu_'+right(@t,4)+'] from datasaldayshpplu_tmp where CONVERT(varchar(6),ddate,112)='''+@t+'''' --exec(@sqlstr) print @sqlstr endelse if right(@t,2)>'12' begin set @t=@t+88 endset @t=@t+1 set @error = @@error +@error endif @error<>0 rollback TRANSACTION else commit TRANSACTION go
set @day=''
set @day=day('2008.01')不知道LZ是否是这个意思
你的是2008.02-2010.03,然后按月建表,一共22张。接下去你想干嘛干嘛...
begin TRANSACTIONdeclare @error intdeclare @maxt varchar(6)
set @maxt=(select max(distinct CONVERT(varchar(6),ddate,112)) from datasaldayshpplu_tmp )declare @mint varchar(6)
set @mint=(select min(distinct CONVERT(varchar(6),ddate,112)) from datasaldayshpplu_tmp )declare @t varchar(6)
set @t=@mintdeclare @sqlstr varchar(500)while @t<=@maxt
beginif right(@t,2)<='12'
begin
set @sqlstr='if object_id('+''''+'DataSalDayShpPlu_'+right(@t,4)+''''+') is not null drop table DataSalDayShpPlu_'+right(@t,4)
+' select * into [DataSalDayShpPlu_'+right(@t,4)+'] from datasaldayshpplu_tmp where CONVERT(varchar(6),ddate,112)='''+@t+''''
--exec(@sqlstr)
print @sqlstr
endelse if right(@t,2)>'12'
begin
set @t=@t+88
endset @t=@t+1
set @error = @@error +@error
endif @error<>0
rollback TRANSACTION
else
commit TRANSACTION
go