SQL Server : Option Clause in Views

I had a task to create MasterCalendar view which will return all the dates within a range. Here is the simplified query that needs to be included the view :

MasterCalendar_2

If you run the above create view statement, sql server will throw the following error:

Incorrect syntax near the keyword ‘option’.

It throws error because views do not allow option clauses. So the workaround would be to create the view without the option clause and when you call the view include the option clause as below:

MasterCalendar_3

PS:
Here is the code from the screen shot:
create view vw_MasterCalendar
as
with cte(N) as
(
select    cast('20120101' as datetime) as N
union all
select    cte.N + 1
from    cte
where    cte.N < cast('20130101' as datetime)
)
select    N as [Date]
from cte
go
select * from vw_MasterCalendar option (maxrecursion 1000)

Advertisements