我正在尝试使用以下查询将SQL查询生成的结果插入临时表:
SELECT *
INTO #YourTempTable
FROM
(WITH cte AS
(
SELECT
sb.case, sb.Trace, sb.Amount, sp.edge, sp.UserId,
count(*) over (partition by sp.edge) as cnt
FROM
Budget sb
JOIN
Sap sp ON sb.Trace = sp.Trace
WHERE
sb.Trace IN (SELECT Trace FROM Sap
WHERE sb.UserId in ('R5', 'HB'))
)
SELECT cte.*
FROM cte
WHERE cnt > 1
ORDER BY edge DESC;
)
我收到错误:
Msg 156,Level 15,State 1,Line 4
关键字'with'附近的语法不正确。Msg 319,Level 15,State 1,Line 4
关键字'with'附近的语法不正确。如果此语句是公用表表达式,xmlnamespaces子句或更改跟踪上下文子句,则必须以分号结束前一个语句。Msg 102,Level 15,State 1,Line 12
')'附近的语法不正确。
任何人都可以告诉我这个吗?
答案 0 :(得分:6)
WITH
早于SELECT
:
with cte as (
select sb.case, sb.Trace, sb.Amount, sp.edge, sp.UserId,
count(*) over (partition by sp.edge) as cnt
from Budget sb join
Sap sp
on sb.Trace = sp.Trace
where sb.Trace in (select Trace from Sap where sb.UserId in ('R5', 'HB'))
)
SELECT *
INTO #YourTempTable
FROM cte
WHERE cnt > 1
ORDER BY edge desc;
注意:Oracle支持您的语法 ,但其他数据库不支持。通常,WITH
应该在声明的最开头。
此外,子查询是不必要的。实际上,您会收到两个额外错误:子查询没有别名,子查询中不允许ORDER BY
(除非使用TOP
或FETCH FIRST
)。