Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

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?
1 Solution
SueloweAuthor Commented:
Many thanks for the quick response ajitha75.  I see I'm not the first to ask this particular question!

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now