[sql-server] How do I create a table based on another table

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.

This question is related to sql-server sql-server-2008 create-table

The answer is


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.


select * into newtable from oldtable

Examples related to sql-server

Passing multiple values for same variable in stored procedure SQL permissions for roles Count the Number of Tables in a SQL Server Database Visual Studio 2017 does not have Business Intelligence Integration Services/Projects ALTER TABLE DROP COLUMN failed because one or more objects access this column Create Local SQL Server database How to create temp table using Create statement in SQL Server? SQL Query Where Date = Today Minus 7 Days How do I pass a list as a parameter in a stored procedure? SQL Server date format yyyymmdd

Examples related to sql-server-2008

Violation of PRIMARY KEY constraint. Cannot insert duplicate key in object How to Use Multiple Columns in Partition By And Ensure No Duplicate Row is Returned SQL Server : How to test if a string has only digit characters Conversion of a varchar data type to a datetime data type resulted in an out-of-range value in SQL query Get last 30 day records from today date in SQL Server How to subtract 30 days from the current date using SQL Server Calculate time difference in minutes in SQL Server SQL Connection Error: System.Data.SqlClient.SqlException (0x80131904) SQL Server Service not available in service list after installation of SQL Server Management Studio How to delete large data of table in SQL without log?

Examples related to create-table

Field 'id' doesn't have a default value? MySQL: Error Code: 1118 Row size too large (> 8126). Changing some columns to TEXT or BLOB Create hive table using "as select" or "like" and also specify delimiter #1064 -You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version C# - Create SQL Server table programmatically How do I create a table based on another table Creating new table with SELECT INTO in SQL create table in postgreSQL PostgreSQL Error: Relation already exists Mysql: Setup the format of DATETIME to 'DD-MM-YYYY HH:MM:SS' when creating a table