• It was not easy to find the difference in the code "noodles" you have, I could not even copy it from browser to my SSMS as it's too wide at the end...

    But finally, I've managed to spot the difference. Your first one uses table variable with CLUSTERED index and another one has not defined it.

    As second example uses heap table SQL will need to do row id lookups (RID's) to get data from it, so it's probably spools the table for this reason.

    As advice, I first I would suggest to use temp table instead of table variable and do not create it separately - use SELECT INTO. You can see if creating clustered index on it will make any difference and if using #table instead of table variable will give you any performance difference.

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]