【发布时间】:2014-06-12 01:46:59
【问题描述】:
在 SQL 中,可以将 WITH 放在 WITH 中吗?
以下面的查询为例,
WITH Temp ([Description], [Amount], [OverdueBy])
AS
(select Description, SUM(Amount) as Amount, (DATEDIFF(day,DueDate,GETDATE())) as OverdueBy from brvAPAllInvoices
Where PaidDate is null and APCo = 1 and Amount > 0
Group By Description, DueDate, APRef
)
select * from Temp
我想根据上述查询创建一个“虚拟”临时表。是否可以使用另一个 WITH 来包含它?
类似这样的事情:
WITH Temp2 ([Description], [Amount], [OverdueBy])
AS
(
WITH Temp ([Description], [Amount], [OverdueBy])
AS
(select Description, SUM(Amount) as Amount, (DATEDIFF(day,DueDate,GETDATE())) as OverdueBy from brvAPAllInvoices
Where PaidDate is null and APCo = 1 and Amount > 0
Group By Description, DueDate, APRef
)
select * from Temp)
select * from Temp2
【问题讨论】:
-
你尝试的时候发生了什么?
-
Msg 156, Level 15, State 1, Line 4 关键字“WITH”附近的语法不正确。消息 319,级别 15,状态 1,第 4 行关键字“with”附近的语法不正确。如果此语句是公用表表达式、xmlnamespaces 子句或更改跟踪上下文子句,则前面的语句必须以分号结束。消息 102,级别 15,状态 1,第 12 行 ')' 附近的语法不正确。
-
确保使用 实际 RDBMS 进行标记。