日期:2014-05-16  浏览次数:20595 次

根据条件循环取出一个月的数据并插入另一张表
declare @tmp int
declare @sql varchar(2000)
set @tmp=1
while @tmp<31
begin
if @tmp<10
begin
set @sql =
'insert Temp_Rpt_List1(表名) (select count(*) from sm_sending_temp_sm_list_all(表名)
where
sendtarget in(select col003 from tbl_sm_rpt_11110(表名)' +cast(@tmp as varchar(10)) + ' where col005=''0'')
and year(latestsendtime)=2011 and month(latestsendtime)=10 and day(latestsendtime)=' +cast(@tmp as varchar(10))+')'
end
else
begin
set @sql ='insert Temp_Rpt_List1 (select count(*) from sm_sending_temp_sm_list_all
where
sendtarget in(select col003 from tbl_sm_rpt_1111' +cast(@tmp as varchar(10)) + ' where col005=''0'')
and year(latestsendtime)=2011 and month(latestsendtime)=10 and day(latestsendtime)=' +cast(@tmp as varchar(10))+')'
end
set @tmp=@tmp+1
--exec (@sql) 执行sql语句
--print @sql 输出sql语句
end