• AFAIK, and someone can correct me if I'm wrong, SQL Server has some problems optimizing plans for CTEs and table variables. Using a temp table might improve the performance, but it's not a sure thing. That's why you need to test.

    I'll try to find a reference to support what I'm saying here, but I'm a little busy, so it won't be immediate.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2