简体   繁体   English

通过复制现有表的结构来创建表

[英]Create Table by Copying Structure of Existing Table

I am trying to create a new table by copying an existing table in SQL Server 2008 using Management Studio. 我试图通过使用Management Studio复制SQL Server 2008中的现有表来创建新表。 The existing table contains no data. 现有表中不包含任何数据。 I am using the following code but am receiving an error for Incorrect Syntax near AS. 我使用以下代码,但在AS附近收到错误语法错误。 I am not sure what is wrong here. 我不确定这里有什么问题。 I am a SQL newb and any help would be appreciated. 我是一个SQL新手,任何帮助将不胜感激。 Thanks. 谢谢。

CREATE TABLE Drop_Centers_Detail
    AS (Select * From Centers_Detail)

like this, however this will not create indexes and constraints 像这样,但是这不会创建索引和约束

select * into Drop_Centers_Detail
from Centers_Detail
where 1 = 0

In Sql Server Managment Studio, right-click your existing table and select Script Table as > Create to > New Query Editor Window . 在Sql Server Managment Studio中,右键单击现有表,然后选择“ Script Table as > Create to > New Query Editor Window This will give you a better starting script that you can use as the base for your new schema. 这将为您提供更好的启动脚本,您可以将其用作新架构的基础。

1) I would suggest generating a create script from the table you want to copy, and then run that on your destination database. 1)我建议从要复制的表中生成一个创建脚本,然后在目标数据库上运行该脚本。

2) Write a Insert statement in another SQL Query window to import that data 2)在另一个SQL Query窗口中编写Insert语句以导入该数据

Insert Into Database1.Table1(Field1, Field2) Select Field1, Field2 From Database2.Table Insert into Database1.Table1(Field1,Field2)从Database2.Table中选择Field1,Field2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM