Link to home
Start Free TrialLog in
Avatar of Suelowe
Suelowe

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of ajitha75
ajitha75
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Suelowe
Suelowe

ASKER

Many thanks for the quick response ajitha75.  I see I'm not the first to ask this particular question!