代码之家  ›  专栏  ›  技术社区  ›  Paul Rowland

如何在单个SELECT语句中包含多个常用表表达式?

  •  101
  • Paul Rowland  · 技术社区  · 17 年前

    宣布一个cte效果很好。

    WITH cte1 AS (
        SELECT * from cdr.Location
        )
    
    select * from cte1 
    

    (这个sql有错误)

    WITH cte1 as (
        SELECT * from cdr.Location
    )
    
    WITH cte2 as (
        SELECT * from cdr.Location
    )
    
    select * from cte1    
    union     
    select * from cte2
    

    Msg 156, Level 15, State 1, Line 7
    Incorrect syntax near the keyword 'WITH'.
    Msg 319, Level 15, State 1, Line 7
    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.
    

    NB。我已尝试输入分号,但出现了此错误

    Msg 102, Level 15, State 1, Line 5
    Incorrect syntax near ';'.
    Msg 102, Level 15, State 1, Line 9
    Incorrect syntax near ';'.
    

    2 回复  |  直到 12 年前
        1
  •  149
  •   Matt Fenwick sagarcool89    14 年前

    WITH 
        cte1 as (SELECT * from cdr.Location),
        cte2 as (SELECT * from cdr.Location)
    select * from cte1 union select * from cte2
    

    基本上, WITH

        2
  •  17
  •   Sagar Dev Timilsina    8 年前

    WITH 
        cte1 as (SELECT * from cdr.Location),
        cte2 as (SELECT * from cdr.Location)
    select * from cte1 union select * from cte2
    

    WITH 
        cte1 as (SELECT * from cdr.Location),
        cte2 as (SELECT * from cte1 where val1 = val2)
    
    select * from cte1 union select * from cte2
    

    val1,val2 只是表达的借口。。

    希望这个博客也能有所帮助: http://iamfixed.blogspot.de/2017/11/common-table-expression-in-sql-with.html