How we can use CTE in subquery in sql server?

Paresh picture Paresh · Dec 16, 2009 · Viewed 41.5k times · Source

How we can use CTE in subquery in sql server?

like ..

select id (i want to use CTE here), name from table_name

Answer

Maximilian Mayerl picture Maximilian Mayerl · Dec 16, 2009

Just define your CTE on top and access it in the subquery?

WITH YourCTE(blubb) AS
(
    SELECT 'Blubb'
)
SELECT id,
       (SELECT blubb FROM YourCTE),
       name
FROM   table_name