maybe if you change the way you create the temp table. Instead, use a table variable:
DECLARE @TibetanYaks TABLE (
YakName char(30) )
INSERT INTO @TibetanYaks (YakID, YakName)
SELECT YakID, YakName
WHERE YakType = 'Tibetan'
I took the sample above from http://www.sqlteam.com/article/temporary-tables
It says that:
- If you have less than 100 rows generally use a table variable. Otherwise use a temporary table. This is because SQL Server won't create statistics on table variables.
- If you need to create indexes on it then you must use a temporary table.
- When using temporary tables always create them and create any indexes and then use them. This will help reduce recompilations. The impact of this is reduced starting in SQL Server 2005 but it's still a good idea.
Hope this helps