47

我想根据另一个表的定义创建一个表。

我来自甲骨文,我通常会这样做:

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

我似乎无法在 SQL Server 2008 中执行此操作。

4

2 回答 2

113

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.

于 2013-08-15T13:48:54.497 回答
18
select * into newtable from oldtable
于 2013-08-15T13:48:44.940 回答