21
with cte as (
    select '2014-03-10 08:00:00' as Dates
    union all
    select '2014-05-11 14:00:00'
)
select * from cte 
join someTable on 1=1 
OPTION (MAXRECURSION 0)

The here above SQL is outputing like a charm all hours between two dates and a field retrieved from a join with another table:

2014-03-10 02:00:00    A
2014-03-10 02:00:00    B
2014-03-10 03:00:00    A
2014-03-10 03:00:00    B
...
2014-05-11 13:00:00    A
2014-05-11 13:00:00    B
2014-05-11 14:00:00    A
2014-05-11 14:00:00    B

I would like to create a view from that but I do not manage to do it. I tried several things but without success. The following is returning : Incorrect syntax near the keyword 'OPTION'.

CREATE VIEW viewName as 
with cte as (
    select '2014-03-10 08:00:00' as Dates
    union all
    select '2014-05-11 14:00:00'
)
select * from cte 
join someTable on 1=1 
OPTION (MAXRECURSION 0)
Marc
  • 9,217
  • 21
  • 67
  • 90
  • 1
    Are you sure you can use variables in a view? –  Nov 27 '14 at 14:28
  • 2
    You can't use variables in a view, if this is what you require you will need to create stored procedure or a function. This looks more like a function to me. – Steve Ford Nov 27 '14 at 14:30

4 Answers4

30

You cannot specify the MAXRECURSION option inside a view.

From http://benchmarkitconsulting.com/colin-stasiuk/2010/04/12/maxrecursion-with-a-cte-in-a-view/:

In order to make use of the MAXRECURSION option you need to first create your view without using the MAXRECURSION option:

USE AdventureWorks;
GO
CREATE VIEW vwCTE AS
--Creates an infinite loop
WITH cte (EmployeeID, ManagerID, Title) as
(
    SELECT EmployeeID, ManagerID, Title
    FROM HumanResources.Employee
    WHERE ManagerID IS NOT NULL
  UNION ALL
    SELECT cte.EmployeeID, cte.ManagerID, cte.Title
    FROM cte
    JOIN  HumanResources.Employee AS e
        ON cte.ManagerID = e.EmployeeID
)
-- Notice the MAXRECURSION option is removed
SELECT EmployeeID, ManagerID, Title
FROM cte
GO

Then when you query the view include the MAXRECURSION option:

USE AdventureWorks;
GO
SELECT  EmployeeID, ManagerID, Title
FROM    vwCTE
OPTION (MAXRECURSION 2);

See also AaskashM's answer at https://stackoverflow.com/a/7428903/195687

Community
  • 1
  • 1
vstrien
  • 2,547
  • 3
  • 27
  • 47
3

If you have more than 100 expected results, and want to avoid having to add the OPTION statement to your VIEW calls, try executing the CTE query - including the OPTION clause - in an OPENQUERY statement within your VIEW.

In your example, it would probably look something like this:

USE AdventureWorks;
GO

CREATE VIEW vwCTE AS
select * from OPENQUERY([YourDatabaseServer], '
--Creates an infinite loop
WITH cte (EmployeeID, ManagerID, Title) as
(
  SELECT EmployeeID, ManagerID, Title
    FROM AdventureWorks.HumanResources.Employee
    WHERE ManagerID IS NOT NULL
  UNION ALL
  SELECT cte.EmployeeID, cte.ManagerID, cte.Title
    FROM cte
    JOIN  AdventureWorks.HumanResources.Employee AS e
      ON cte.ManagerID = e.EmployeeID
)
-- Notice the MAXRECURSION option is removed
SELECT EmployeeID, ManagerID, Title
  FROM cte
  OPTION (MAXRECURSION 0)
' ) x
GO

Notice that you must fully-qualify object references, i.e. the database and user specifications must prefix the object (table, view, sproc, or function) references.

Sure, it's a little ugly, but gets the job done nicely, and avoids having to add that pesky OPTION clause.

jskipb
  • 71
  • 5
0

If you can put a limit on the number of hours you need you can remove the recursion

CREATE VIEW viewName AS 
    WITH TenNumbers AS (
        SELECT Number
        FROM (VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) AS T(Number)
    )
    ,Numbers AS (
        SELECT ROW_NUMBER() OVER (ORDER BY T10.Number) AS Number
        FROM TenNumbers AS T10
             CROSS JOIN TenNumbers AS T100
             CROSS JOIN TenNumbers AS T1000
             -- ...
    )
    SELECT DATEADD(hour, Number - 1, '20140310 08:00:00') AS Dates
    FROM Numbers
adrianm
  • 14,468
  • 5
  • 55
  • 102
-3

Try,

CREATE VIEW vw_your_view
AS
WITH TABLE1(a,b,c) 
as (
SELECT a,b,c from Table 
)

where a, b, c are your table fields.

Have a nice day!

Nicholas Gentile
  • 1,512
  • 1
  • 9
  • 16
  • 1
    This is a copy of the OP's code with columns changed and OPTION removed. While this satisfies the title of the question it does not answer the problem within the question. – ShellNinja Mar 25 '22 at 13:24