我正在尝试使用以下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);
但我收到以下消息:
“Msg 156,Level 15,State 1,Procedure v_month_days,Line 15 [Batch Start Line 0] 关键字'option'“
附近的语法不正确
我还有其他选择吗?
答案 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);