没看明白,能不能说详细点呢
Quote: 引用 3 楼 yupeigu 的回复: 没看明白,能不能说详细点呢 就是要做个留存语句 要求做出来的结果根据日期做循环 每天使用同样的sql语句做循环计算出留存,然后计算出的结果显示在一个表中
declare @start_date varchar(10) declare @end_date varchar(10) set @start_date = '2013-10-01' set @end_date = '2013-11-11' select dateadd(day,number,@start_date) as biz_date from master..spt_values t where t.type ='P' and dateadd(day,number,@start_date) <= @end_date /* biz_date 2013-10-01 00:00:00.000 2013-10-02 00:00:00.000 2013-10-03 00:00:00.000 2013-10-04 00:00:00.000 2013-10-05 00:00:00.000 2013-10-06 00:00:00.000 2013-10-07 00:00:00.000 2013-10-08 00:00:00.000 2013-10-09 00:00:00.000 2013-10-10 00:00:00.000 2013-10-11 00:00:00.000 2013-10-12 00:00:00.000 2013-10-13 00:00:00.000 2013-10-14 00:00:00.000 2013-10-15 00:00:00.000 2013-10-16 00:00:00.000 2013-10-17 00:00:00.000 2013-10-18 00:00:00.000 2013-10-19 00:00:00.000 2013-10-20 00:00:00.000 2013-10-21 00:00:00.000 2013-10-22 00:00:00.000 2013-10-23 00:00:00.000 2013-10-24 00:00:00.000 2013-10-25 00:00:00.000 2013-10-26 00:00:00.000 2013-10-27 00:00:00.000 2013-10-28 00:00:00.000 2013-10-29 00:00:00.000 2013-10-30 00:00:00.000 2013-10-31 00:00:00.000 2013-11-01 00:00:00.000 2013-11-02 00:00:00.000 2013-11-03 00:00:00.000 2013-11-04 00:00:00.000 2013-11-05 00:00:00.000 2013-11-06 00:00:00.000 2013-11-07 00:00:00.000 2013-11-08 00:00:00.000 2013-11-09 00:00:00.000 2013-11-10 00:00:00.000 2013-11-11 00:00:00.000 */
declare @start_date datetime declare @end_date datetime set @start_date = '2013-10-01' --循环的开始时间 set @end_date = '2013-11-11' --循环的结束时间 --开始循环 while @start_date <= @end_date begin --模拟你要执行的语句 select @start_date set @start_date = DATEADD(day,1,@start_date) end
或者这个: declare @start_date datetime declare @end_date datetime set @start_date = '2013-10-01' --循环的开始时间 set @end_date = '2013-11-11' --循环的结束时间 --开始循环 while @start_date <= @end_date begin --模拟你要执行的语句 select @start_date set @start_date = DATEADD(day,1,@start_date) end
if object_id('viewinfo') is not null drop table viewinfo create table viewinfo (Dt DateTime ,LogSql varchar(200) , Result varchar(200)) go declare @start_date datetime declare @end_date datetime set @start_date = GETDATE () --当前时间 set @end_date = DATEADD(HOUR ,1,GETDATE ()) --每天默认执行1小时 --开始循环 while @start_date <= @end_date begin --模拟你要执行的语句 insert into viewinfo select @start_date,'update test....','select ''' set @start_date = DATEADD(MINUTE,1,@start_date) end select * from viewinfo Dt LogSql Result ----------------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --2013-12-11 10:40:37.563 update test.... select '