declare @n int
declare @begtime datetime
declare @endtime datetimeset @begtime='1999-06-24'
set @endtime='1999-07-06'
set @n=(select datediff(day,@begtime,@endtime))
print @ndeclare @tab table(kk datetime)while @n>0
begin
--print dateadd(day,@n,@begtime)insert @tab values(dateadd(day,@n,@begtime))
set @n=@n-1
end
select * from @tab

解决方案 »

  1.   

    不过可以用cross join 删除掉不在该时间段的日期,这样的生成速度比用while循环要快
    年 cross join 月 cross join 日,要处理好月末日期的关系
      

  2.   

    以下方式可以生成最多10000天(若需超过此限制,稍作修改即可)的任意期间内的日历表。declare @BDate datetime,@EDate datetime
    set @BDate ='1999-06-24'
    set @EDate = '1999-07-06'select dateadd(day,id-1,@BDate) from (
    select id=a.id+b.id*10+c.id*100+d.id*1000+1 from 
    (
    select id=0 union all select 1
    union all select id=2 union all select 3
    union all select id=4 union all select 5
    union all select id=6 union all select 7
    union all select id=8 union all select 9
    ) a,(
    select id=0 union all select 1
    union all select id=2 union all select 3
    union all select id=4 union all select 5
    union all select id=6 union all select 7
    union all select id=8 union all select 9
    ) b,(
    select id=0 union all select 1
    union all select id=2 union all select 3
    union all select id=4 union all select 5
    union all select id=6 union all select 7
    union all select id=8 union all select 9
    ) c,(
    select id=0 union all select 1
    union all select id=2 union all select 3
    union all select id=4 union all select 5
    union all select id=6 union all select 7
    union all select id=8 union all select 9
    ) d
    ) aa
    where id<=datediff(day,@BDate,@EDate)+1
    order by id
      

  3.   

    drop FUNCTION dbo.f_getdate
    CREATE FUNCTION dbo.f_getdate(
    @state datetime,    --开始时间
    @end datetime       --结束时间
    )RETURNS @re TABLE(T_Date datetime)
    AS
    begin
      DECLARE @i int
      set @i=datediff(dd,@state,@end)
      while @i>0 
      begin
        INSERT @re values(@state)
        select @state=dateadd(dd,1,@state)
        set @i=@i-1
      end 
      INSERT @re values(@end)
      RETURN
    end select * from dbo.f_getdate('1999-06-24','1999-07-06')
      

  4.   

    create function fnt_getDts(@tdt1 datetime,@tdt2 datetime)
    returns @re table(dt datetime)
    as
    /*
    功能:返回两个日期之间结果集
    */
    begin
    while @tdt1<=@tdt2 
        begin
    --if  datepart(dw,@tdt1)<>1 and datepart(dw,@tdt1)<>7 这里可过滤筛选日期。
    insert into @re
    select @tdt1
    select @tdt1=dateadd(d,1,@tdt1)
        end
    return
    end
    go
      

  5.   

    --测试语句
    declare @StartTime as datetime
    set @StartTime='2005-6-1'
    declare @t table(dDate datetime) --计算连续时间
    while @StartTime<=getdate()
    begin
     insert into @t select @StartTime
     set @StartTime=Dateadd(day,1,@StartTime)
    end
    --测试结果
    dDate                                                  
    ------------------------------------------------------ 
    2005-06-01 00:00:00.000
    2005-06-02 00:00:00.000
    2005-06-03 00:00:00.000
    2005-06-04 00:00:00.000
    2005-06-05 00:00:00.000
    2005-06-06 00:00:00.000
    2005-06-07 00:00:00.000
    2005-06-08 00:00:00.000
    2005-06-09 00:00:00.000
    2005-06-10 00:00:00.000
    2005-06-11 00:00:00.000
    2005-06-12 00:00:00.000
    2005-06-13 00:00:00.000
    2005-06-14 00:00:00.000
    2005-06-15 00:00:00.000
    2005-06-16 00:00:00.000
    2005-06-17 00:00:00.000
    2005-06-18 00:00:00.000
    2005-06-19 00:00:00.000
    2005-06-20 00:00:00.000
    2005-06-21 00:00:00.000
    2005-06-22 00:00:00.000
    2005-06-23 00:00:00.000
    2005-06-24 00:00:00.000
    2005-06-25 00:00:00.000
    2005-06-26 00:00:00.000
    2005-06-27 00:00:00.000
    2005-06-28 00:00:00.000
    2005-06-29 00:00:00.000
    2005-06-30 00:00:00.000
    2005-07-01 00:00:00.000(所影响的行数为 31 行)
      

  6.   

    declare @date datetime
    set @date = '1999-06-24'
    while (@date >='1999-06-24') and (@date<='1999-07-06')
    begin
       print convert(varchar(10),@date,21)
       set @date = convert(varchar(10),dateadd(day,1,@date),21) 
    end