-1

我想在我的查询中使用多个 CTE,但我无法让它工作。以下是我想做的一个例子:

WITH tbl1 AS
    (SELECT *
    FROM tblStuff)

WITH tbl2 AS
    (SELECT tbl1.fldStuff1 ...
     FROM tbl1, tblStuff2)

    SELECT *
    FROM tbl2
4

2 回答 2

14

您只需指定WITH一次。然后,您可以列出用逗号分隔的其他 CTE。例如:

WITH cte1 AS 
(
  ...
),
cte2 AS 
(
  ...
)
SELECT ...;
于 2013-07-19T14:34:25.643 回答
8
WITH tbl1 AS
    (SELECT *
    FROM tblStuff),

tbl2 AS
    (SELECT tbl1.fldStuff1, tbl1.fldStuff2, tblStuff2.fldStuff1, tblStuff2.fldStuff2
     FROM tbl1, tblStuff2)

    SELECT *
    FROM tbl2

当您使用多个 CTE 时,不需要额外的 WITH,用逗号分隔。

于 2013-07-19T14:34:55.530 回答