views:

532

answers:

2

How we can use CTE in subquery in sql server?

like ..

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

+5  A: 

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
Maximilian Mayerl
Thanks for your effort
Paresh
+3  A: 

There is a very good article on CTE's here (http://www.4guysfromrolla.com/webtech/071906-1.shtml). I have referred back to this article many times in learning how to get the most from CTE's.

Randy

Randy Minder
Thanks for the article
priyanka.sarkar