Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Invalid object name '#TmpTable' whenselecting from temporary table made using INTO from exec(@str)

$
0
0

Hello,

 

Can anyone shed some light on why the following:

 

declare @str    varchar(2000)

set @str = 'SELECT * INTO #TmpTable FROM FormHistory'
exec (@str)

SELECT * FROM #TmpTable

 

gives the following error:

 

Invalid object name '#TmpTable'.

 

This is a very cutdown version of what I am trying to achieve so it might not seem obvious why I am writing it into a string and using exec but in the real code I do need to do this. I have cut it right back to try to get to the bottom of why this doesn't work. I suspect the # in the string is causing the problems.

 

Thanks for any help

 


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>