简体   繁体   English

SQL Server:WITH AS使用不正确的语法错误

[英]SQL Server : WITH AS USING Incorrect Syntax Error

WITH t(num) AS (SELECT MAX(stok) FROM cd)

WITH y AS (SELECT cdno FROM cd,t WHERE cd.stok IN (t.num))

select * from y

I have this code thread. 我有这个代码线程。 But is is not correct, I have some mistakes. 但是是不正确的,我有一些错误。 I do not why please help . 我不为什么请帮助。

When I am using with line 1 and select * from t is working but in this type that's not working . 当我与第1行一起使用并select * from t有效,但在这种情况下不起作用。

You cannot have two CTE's (Common Table Expression) after another like this. 您不能像这样再接两个CTE(公用表表达式)。 If you to define two CTE, you need to use this syntax: 如果要定义两个 CTE,则需要使用以下语法:

WITH t(num) AS 
(
    SELECT MAX(stok) 
    FROM cd
), y AS 
(
    SELECT cdno 
    FROM cd, t 
    WHERE cd.stok IN (t.num)
)
SELECT * 
FROM y

You can chain multiple CTE's after one another by separating them with a comma , and just leaving out the WITH keyword for the subsequent CTE's. 你可以将多个 CTE的彼此中间用逗号隔开后,和刚刚离开了WITH关键字为后续的CTE的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM