简体   繁体   中英

SQL Server - Scripting a table

I want to create a clone table(same table structure) with an already existing table. I know one of the ways is :

Right click on table name -> Script table as -> Create To -> New query editor

I also found out another way would be:

Right click on database name -> Tasks -> Generate scripts -> choose the tables you want

Along the way, you can set scripting options to script whatever I want such as constraint,indexes,triggers,etc.

So what would be the difference between these two methods? As far I tried both method and both produce the same outcome, or at least that's what I see.

I do realize that all the STATISTICS in a table cannot be scripted if I opt for the first method but if I go for the second method, I could script all the statistics to the clone table by setting the statistic settings to script stats and histograms as well. This would not be possible in the first method.

Can someone shed some light on this?

Thanks

As per my Knowledge Both are same. Ie. Create To-> New Query window also generates the script but it only allowers you to create the table - You Can't specify what all need to be included in the create script since this is a default template. It will by default generate the scripts for all the constraints available in the table.

Whereas Using the Generate Scripts option, you will have more control over the script that is being generated, Like you can Generate the script for the inserts, You can choose whether generate the script for keys and constraints or not etc.

So If you just want to generate the Create script, then you can right-click the table and choose to create to --> New Query window or If you wish to have more control over the process then maybe try the Generate scripts option.

Also, If your requirement is just a table with the same structure as well as data - Without the constraints, Then you can try the Select * INTO option

SELECT * INTO T2 FROM T1

Please visit these link for more info Generate Scripts for Data in the Tables of SQL Server

Options for scripting SQL Server database objects

With below option you will not be able to generate the script to copy the data

Right click on table name -> Script table as -> Create To -> New query editor

But if you are generating the script from Task, you will be able to copy structure as well as data

Right click on database name -> Tasks -> Generate scripts -> choose the tables you want

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