chris
chris

Reputation: 37460

T-SQL: How can you create a table with SELECT?

In oracle, you can issue:

 create table foo as select * from bar;

What is the equivalent T-SQL statement?

Upvotes: 39

Views: 80775

Answers (4)

rchacko
rchacko

Reputation: 2119

select * into dbo.my_table from (
    select col1, col2... coln from bla
)tbl

Upvotes: 1

scarpacci
scarpacci

Reputation: 9194

If you want to write to the tempdb

Select *
INTO #tmp
From bar

or to a SQL DB

Select *
INTO Temp
From bar

Upvotes: 5

CaffGeek
CaffGeek

Reputation: 22054

You can try like this:

select * into foo from bar

Upvotes: 32

Oded
Oded

Reputation: 499002

You can use SELECT INTO. From MSDN:

The SELECT INTO statement creates a new table and populates it with the result set of the SELECT statement. SELECT INTO can be used to combine data from several tables or views into one table. It can also be used to create a new table that contains data selected from a linked server.

So:

SELECT col1, col2, col3 INTO newTable FROM existingTable;

Upvotes: 55

Related Questions