2016-12-28 61 views
0

我試圖創建一個視圖以下SQL與條款「選項」創建視圖包括

create view v_month_days 
    as 
    with C(TheDate) as 
    (
     select CAST('01/01/2014' AS DATETIME) 
     union all 
     select dateadd(day, 1, C.TheDate) 
     from C 
     where C.TheDate < CONVERT(VARCHAR (8), (DATEADD(yy, DATEDIFF(yy,0,getdate()) + 1, -1)),112) 
    ) 
    select 
    cast(dateadd(month, datediff(month, 0, C.TheDate), 0) as date) as Month, 
    count(*) as DayCount 
    from C 
    group by dateadd(month, datediff(month, 0, C.TheDate), 0) 
    option (maxrecursion 0); 

但是我收到以下消息:

「消息156,15級,狀態1,過程v_month_days,行15批起始線0] 錯誤附近關鍵字「選項」」

我還有什麼別的選擇語法?

回答

1

在沒有Option聲明的情況下先創建視圖。

create view v_month_days 
    as 
    with C(TheDate) as 
    (
     select CAST('01/01/2014' AS DATETIME) 
     union all 
     select dateadd(day, 1, C.TheDate) 
     from C 
     where C.TheDate < CONVERT(VARCHAR (8), (DATEADD(yy, DATEDIFF(yy,0,getdate()) + 1, -1)),112) 
    ) 
    select cast(dateadd(month, datediff(month, 0, C.TheDate), 0) as date) as Month, 
    count(*) as DayCount 
    from C 
    group by dateadd(month, datediff(month, 0, C.TheDate), 0) 

然後,當您從視圖中選擇時,使用Option語句。

Select Month, DayCount From v_month_days 
Option (maxrecursion 0); 
+0

感謝它完美的作品 – ipeoples