CREATE TABLE #tempTable
(
	  Col01 VARCHAR(10)
	, Col02 INT
)

INSERT INTO #tempTable(Col01,Col02)VALUES('s1',1)
INSERT INTO #tempTable(Col01,Col02)VALUES('s2',2)
INSERT INTO #tempTable(Col01,Col02)VALUES('s3',3)

SELECT * FROM #tempTable
DROP TABLE #tempTable
IF OBJECT_ID('tempdb..#tempTable') IS NOT NULL
	DROP TABLE #tempTable

CREATE TABLE #tempTable
(
	  Col01 VARCHAR(10)
	, Col02 INT
)

By using SELECT … INTO, you don’t have to define the columns of the temporary table. The temporary table columns will be based on the SELECT columns.

SELECT
       col01
     , col02
     , col03
INTO #TempTable
FROM
	tbTableName

SELECT * FROM #TempTable

DROP TABLE #TempTable
Last modified: September 11, 2019

Author

Comments

Write a Reply or Comment