SQL Server WITH statement

My goal is to select result from one CTE and insert into other table with another CTE in the same procedure. How to do it?

My error is…

invalid object name xy.

My query is

WITH ds
(
    Select a, b, c 
    from test1    
),
xy
(
    select d, e, f 
    from test2 
    where (uses conditions from ds)    
)
Select * 
from ds  (the result set of ds, am exporting this to csv)

Insert into AuditTest
(
  Select * from xy
)

Leave a Comment