Nestor
Nestor

Reputation: 13990

How to force NOEXPAND on a Common Expression Table (CTE)?

When I run the following query, the dbo.VerySlowTableValuedFunction() gets called many times. How can I modify it so that the slow function gets called only once.

with DatesT as (
    ....list of dates....
), slowT as 
(
     select *
     from dbo.VerySlowTableValuedFunction()
)
select DateS, (
     select top 1 [dataseries] 
     from [dataseries] 
     where DateS = p.DateS
     order by DateS desc
 ) as slowData
 from DatesT p

Upvotes: 0

Views: 684

Answers (1)

Nestor
Nestor

Reputation: 13990

There is not way to do it as of SQL Server 2008. This has been requested as a feature... but it's not clear it'll be included in Denali.

Upvotes: 1

Related Questions