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
This entry was posted in Joy of T-SQL. Bookmark the permalink.

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s