TSQL Writing into a Temporary Table from Dynamic SQL

后端 未结 8 1806
自闭症患者
自闭症患者 2020-12-11 04:55

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         


        
8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-11 05:21

    Another method is to use all code inside the dynamic SQL

    SET @SQL1 = 'SELECT * INTO #temp WHERE ...
    SELECT * from #temp  ' 
    exec(@SQL1) 
    

提交回复
热议问题