嵌套的WITH语句

时间:2012-03-05 06:45:10

标签: sql-server tsql

有效:

; with res1 as (
        select 1 as col1
    )
select * from res1

如何将WITH语句嵌套在另一个中?我试过了

; with res1 as (
    ; with res2 as (
        select 1 as col1
    ) 
    select * from res2
    )
select * from res1

但是我收到了错误:

Msg 102, Level 15, State 1, Line 2
Incorrect syntax near ';'.
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near ')'.

1 个答案:

答案 0 :(得分:7)

你可以这样做:

; with res1 as (
        select 1 as col1
    ),
    res2 as(
    select * from res1
    )
select * from res2