Create Temp Table from a Select Statement in SQL Server

Performing a select into in order to create a table from another table is SQL Server is very straight forward.
SELECT * into #TMP_MY_TEMP_TABLE
select * from MyTable 
If you want to create a table dynamically from a select statement  where you are joining multiple tables just add a table alias in the second select statement.  
SELECT * into #TMP_MY_TEMP_TABLE
(select a.field1, a.field2, a.field3, b.fieldx
 from MyTableA a, MyTableB B
 where a.field1 = b.field1 
 ) TableX -- An Alias is required to perform a SELECT INTO
Comments are closed