I have a temp table which aggregates columns from multiple tables.
I would like to convert this temp table into a permanent table without explicit specifying the column names and their types.
Don't know if i have explained this well enough
You can use SELECT ... INTO
:
SELECT *
INTO dbo.normal_table
FROM #temp_table
-- WHERE 1 = 2; --add if you only want table structure and not actual data
Things to check after table creation:
IDENTITY
column and current value (may need reseeding)DEFAULT
values of column(if temp table has defaults, normal table needs to be ALTER
ed)COLLATION
tempdb may have different collation than your databaseVARCHAR
,NVARCHAR
,CHAR
, DECIMAL
..) if SELECT
contains expressionsIf temp table does not contain IDENTITY
column you can add one using:
SELECT ID = IDENTITY(INT,1,1)
,col1 = NULL -- NULL by default are treated as INT so you may need casting
,col2 = CAST(NULL AS DECIMAL(38,10))
,t.*
INTO dbo.table_name
FROM #temp t
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With