Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nested WITH statement

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 ')'.
like image 396
Alexei Avatar asked Feb 22 '23 06:02

Alexei


1 Answers

you can do it as follows:

; with res1 as (
        select 1 as col1
    ),
    res2 as(
    select * from res1
    )
select * from res2
like image 117
Vikram Avatar answered Apr 27 '23 12:04

Vikram