简体   繁体   中英

How to create a new table from existing table with both keys and data in SQL Server 2012 (Management Studio)?

How can I create a new table from an existing one in SQL Server 2012 , with both its keys and its data ?

This :

select * into New_Table
from Old_Table

Does not create the dependencies .

And this :

In Sql Server Managment Studio, right-click your existing table and select Script Table as > Create to > New Query Editor Window

Doesn't get the data .

Thanks

The following instructions work well if you want a GUI based solution and are dealing with a moderate amount of data.

  1. In SSMS, right-click on the database that contains the table and select Generate Scripts
  2. Select the specific table(s) to recreate

    选择对象

  3. Select the Save to new query window radio button and click Advanced to set scripting options for the table

    高级脚本选项

  4. Set the Types of data to script property to Schema and data

    脚本的数据类型

  5. Modify the table name in the resulting script as appropriate

One way is to use something like SQL Server Data Tools (SSDT). This is a free, Microsoft, add-on to Visual Studio 2012/2013 (which you don't need to own). This tool will do schema and data compares. So all dependencies and schema objects will be created for you, in the correct order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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