4

That works:

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

How to nest a WITH statement one inside another? I tried

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

but I get an error:

Msg 102, Level 15, State 1, Line 2
Incorrect syntax near ';'.
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near ')'.
nneonneo
  • 171,345
  • 36
  • 312
  • 383
Alexei
  • 1,289
  • 3
  • 19
  • 34

1 Answers1

8

you can do it as follows:

; with res1 as (
        select 1 as col1
    ),
    res2 as(
    select * from res1
    )
select * from res2
Vikram
  • 8,235
  • 33
  • 47