Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a table based on another table [duplicate]

I want to create a table based on the definition of another table.

I'm coming from oracle and I'd normally do this:

 CREATE TABLE schema.newtable AS SELECT * FROM schema.oldtable; 

I can't seem to be able to do this in SQL Server 2008.

like image 300
oliverdejohnson Avatar asked Aug 15 '13 13:08

oliverdejohnson


1 Answers

There is no such syntax in SQL Server, though CREATE TABLE AS ... SELECT does exist in PDW. In SQL Server you can use this query to create an empty table:

SELECT * INTO schema.newtable FROM schema.oldtable WHERE 1 = 0; 

(If you want to make a copy of the table including all of the data, then leave out the WHERE clause.)

Note that this creates the same column structure (including an IDENTITY column if one exists) but it does not copy any indexes, constraints, triggers, etc.

like image 168
Aaron Bertrand Avatar answered Sep 28 '22 03:09

Aaron Bertrand