;with dc as
(
select * from t1
);with dc2 as
(
select * from t1
)
我连续这样用 ,连编译都不通过。。

解决方案 »

  1.   

    ;with dc as
    (
    select * from t1
    ), dc2 as
    (
    select * from t1
    )
      

  2.   

    不同于派生表,cte不能被直接嵌套,你不能在一个cte内定义另一个cte,但是你可以用同一个with语句定义多个cte,每个cte都可以引用前面的cte外部查询可以访问所有的cte。
      

  3.   

    ;with dc as
    (
    select * from t1
    ),
     dc2 as
    (
    select * from t1
    )
      

  4.   

    报错
    Msg 156, Level 15, State 1, Line 38
    Incorrect syntax near the keyword 'with'.
    Msg 319, Level 15, State 1, Line 38
    Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.;with dc as
    (
    select * from t1
    ), with dctotal as
    (
    select * from t2
    )我上面那样用的
      

  5.   

    不过我不知道这样用有什么意义 因为CTE是基于本次作用域的
      

  6.   

    一个with就可以了,with表达式只需要一个
      

  7.   

    ;WITH 
    EmpOrdersCTE (EmployeeID, NumOrders)
    AS
    (
      SELECT EmployeeID, COUNT(*)
      FROM Orders
      GROUP BY EmployeeID
    ),
    MinMaxOrdersCTE (Mn, Mx, Diff)
    AS
    (
      SELECT MIN(NumOrders), MAX(NumOrders), AVG(NumOrders)
      FROM EmpOrdersCTE
    )
    SELECT Mn, Mx,    Diff
    FROM MinMaxOrdersCTE
    --例,引用另外一个cte的cte