Insert Data Into Temp Table with Query Insert Data Into Temp Table with Query sql sql

Insert Data Into Temp Table with Query


SELECT *INTO #TempFROM  (SELECT     Received,     Total,     Answer,     (CASE WHEN application LIKE '%STUFF%' THEN 'MORESTUFF' END) AS application   FROM     FirstTable   WHERE     Recieved = 1 AND     application = 'MORESTUFF'   GROUP BY     CASE WHEN application LIKE '%STUFF%' THEN 'MORESTUFF' END) dataWHERE  application LIKE    isNull(      '%MORESTUFF%',      '%')


SQL Server R2 2008 needs the AS clause as follows:

SELECT * INTO #tempFROM (    SELECT col1, col2    FROM table1) AS x

The query failed without the AS x at the end.


EDIT

It's also needed when using SS2016, had to add as t to the end.

 Select * into #result from (SELECT * FROM  #temp where [id] = @id) as t //<-- as t


Fastest way to do this is using "SELECT INTO" command e.g.

SELECT * INTO #TempTableNameFROM....

This will create a new table, you don't have to create it in advance.