Consider the following code:
SET @SQL1 = \'SELECT * INTO #temp WHERE ...\' exec(@SQL1) SELECT * from #temp (this line throws an error that #temp doesn\'t ex
Another method is to use all code inside the dynamic SQL
SET @SQL1 = 'SELECT * INTO #temp WHERE ... SELECT * from #temp ' exec(@SQL1)