I have a stored procedure with multiple CTE's where I am trying to insert the outcome in to a table. I have tried following the guidance here Combining INSERT INTO and WITH/CTE but having no luck. Could someone advise where to put the INSERT please?
AS BEGIN
With CTE1 As (
Query
),
CTE2 As (
Query
),
CTE3 As (
Query
)
INSERT INTO dbo.table
(
Fields
)
Select * From
(
Select * from CTE1
Union
Select * from CTE2
Union
Select * from CTE3
) as A
END
;