Best way to create a temp table with same columns and type as a permanent table

前端 未结 6 589
长发绾君心
长发绾君心 2021-01-30 04:03

I need to create a temp table with same columns and type as a permanent table. What is the best way to do it? (The Permanent table has over 100 columns)

i.e.

Usu

6条回答
  •  [愿得一人]
    2021-01-30 04:35

    Clone Temporary Table Structure to New Physical Table in SQL Server

    we will see how to Clone Temporary Table Structure to New Physical Table in SQL Server.This is applicable for both Azure SQL db and on-premises.

    Demo SQL Script

    IF OBJECT_ID('TempDB..#TempTable') IS NOT NULL
        DROP TABLE #TempTable;
    
    SELECT 1 AS ID,'Arul' AS Names
    INTO
    #TempTable;
    
    SELECT * FROM #TempTable;
    

    METHOD 1

    SELECT * INTO TempTable1 FROM #TempTable WHERE 1=0;
    
    EXEC SP_HELP TempTable1;
    

    METHOD 2

    SELECT TOP 0 * INTO TempTable1 FROM #TempTable;
    
    EXEC SP_HELP TempTable1;
    

提交回复
热议问题