Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

tsql

oracle

In oracle, you can issue:

 create table foo as select * from bar; 

What is the equivalent T-SQL statement?

like image 964
chris Avatar asked Feb 08 '10 19:02

chris


People also ask

How do I create a SQL table with SELECT statements?

You can create one table from another by adding a SELECT statement at the end of the CREATE TABLE statement: CREATE TABLE new_tbl [AS] SELECT * FROM orig_tbl; MySQL creates new columns for all elements in the SELECT .

Can I create a table from a query?

On the Design tab, in the Query Type group, click Make Table. The Make Table dialog box appears. In the Table Name box, enter a name for the new table. Click the down-arrow and select an existing table name.

Does SELECT into always create a table?

Description. SELECT INTO creates a new table and fills it with data computed by a query. The data is not returned to the client, as it is with a normal SELECT . The new table's columns have the names and data types associated with the output columns of the SELECT .

What is SELECT T * in SQL?

SQL Server SELECT statement is used to fetch the data from a database table which returns data in the form of result table. These result tables are called result-sets.


2 Answers

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; 
like image 176
Oded Avatar answered Oct 06 '22 18:10

Oded


You can try like this:

select * into foo from bar 
like image 28
CaffGeek Avatar answered Oct 06 '22 20:10

CaffGeek