TSQL Writing into a Temporary Table from Dynamic SQL

Posted by Jeff on Stack Overflow See other posts from Stack Overflow or by Jeff
Published on 2009-03-27T01:16:33Z Indexed on 2010/06/08 8:02 UTC
Read the original article Hit count: 163

Consider the following code:

SET @SQL1 = 'SELECT * INTO #temp WHERE ...'
exec(@SQL1)
SELECT * from #temp  (this line throws an error that #temp doesn't exist)

Apparently this is because the exec command spins off a separate session and #temp is local to that session. I can use a global temporary table ##temp, but then I have to come up with a naming scheme to avoid collisions. What do you all recommend?

© Stack Overflow or respective owner

Related posts about sql-server

Related posts about tsql