SELECT INTO

 

USE TestDB;
GO

SELECT * 
     INTO #testtable 
     FROM Sales.OrderDetails;
GO

SELECT * 
     FROM #testtable;
GO
SELECT orderid id
     , qty
     ,SUM(discount) OVER (PARTITION BY orderid) summy
     INTO #test
     FROM #testtable;
GO

SELECT * 
     FROM #Test
GO

We use SELECT INTO instead of create a new table and insert data into it.If we just wanna to create a copy of a table just like bellow query use where clause with false statement.It creates but not populated.

SELECT * 
     INTO #testtable2
     FROM Sales.OrderDetails
        WHERE 1=2;
GO

SELECT * 
     FROM #testtable2;
GO
Advertisements