Mar*_*arc 9 sql t-sql sql-server
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)
Run Code Online (Sandbox Code Playgroud)
上面的SQL在两个日期之间的所有小时和从另一个表的连接中检索的字段输出就像一个魅力:
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
Run Code Online (Sandbox Code Playgroud)
我想从中创建一个视图,但我没有设法做到这一点.我尝试了几件事但没有成功.以下是返回: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)
Run Code Online (Sandbox Code Playgroud)
vst*_*ien 12
您无法MAXRECURSION在视图中指定选项.
来自http://benchmarkitconsulting.com/colin-stasiuk/2010/04/12/maxrecursion-with-a-cte-in-a-view/:
要使用MAXRECURSION选项,您需要先创建视图,而不使用MAXRECURSION选项:
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
然后在查询视图时包含MAXRECURSION选项:
USE AdventureWorks; GO SELECT EmployeeID, ManagerID, Title FROM vwCTE OPTION (MAXRECURSION 2);
另请参阅AaskashM的答案,网址为/sf/answers/520023241/
| 归档时间: |
|
| 查看次数: |
20471 次 |
| 最近记录: |