Using a variable to reference a table name in T-SQL and accessing the resultant temporary table.

I have constructed a string of dynamic SQL which concatenates a select statement with a variable containing the name of the table I wish to interrogate.  This works fine and I can see the results clearly.  My problem is that I need to store the results of my query in a temporary table (which I've called #temp1)  in order to further manipulate the data; the 'select into' statement works fine, but the following query is failing with the message 'Invalid object name #temp1'.  Does anybody have a solution which would enable me to access the temporary table please?  Perhaps an alternative way of declaring/setting/using a variable as a table name?
Who is Participating?
SueloweAuthor Commented:
Many thanks for the quick response ajitha75.  I see I'm not the first to ask this particular question!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.