下面的两组语句,第一个不出错,第二个出错。什么原因?
DECLARE @STARTDATE datetime; 
DECLARE @EntDt datetime; 
set @STARTDATE = '01/01/2009';  
set @EntDt = '12/31/2009'; 
declare @dcnt int; 
;with DateList as   
 (   
    select @STARTDATE DateValue   
    union all   
    select DateValue + 1 from    DateList      
    where   DateValue + 1 < convert(VARCHAR(15),@EntDt,101)   
 )   
  select count(*) as DayCnt from (   
  select DateValue,DATENAME(WEEKDAY, DateValue ) as WEEKDAY from DateList
  where DATENAME(WEEKDAY, DateValue ) not IN ( 'Saturday','Sunday' )     
  )a
option (maxrecursion 365);;with cet as 
(select 1 s union all
select s from cet)
select *  from cet
option(maxrecursion 365);

解决方案 »

  1.   

    select '01/01/2009'+1
    就是错的,应该用
    dateadd(dd,1,'01/01/2009')
      

  2.   

    呵呵...
    没看到后面用了option(maxrecursion 365)
      

  3.   

    递归执行的次数超过了 maxrecursion 限定的次数,sql server 便会提示错误。with t as (
    select 1 num, 1 cnt
    union all
    select num,cnt+1 from t where cnt<365
    )
    select num from t
    option (maxrecursion 365);