HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

How to clone a table schema (including indices) in SQL Server?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
indicessqlschemaincludinghowservertableclone

Problem

Is there a command that will completely clone the schema of a table? This would include all constraints, foreign keys, and indices on the table.

In SSMS the Script Table as | Create To menu command does not include indices defined on the table.

Solution

To change the behavior of Script Table As, use Tools | Options | SQL Server Object Explorer | Scripting | Table and view options as Scott Hodgin originally suggested in a comment.

Alternatively, if you want to script out multiple objects at the same time, you want to use the Generate Scripts functionality. Right click on the database in question, select Tasks --> Generate Scripts. Then the wizard will walk you through further options. You can even have it script out the data.

You can select all objects of a type, or specific objects to be scripted. By default, some parts of the database aren't scripted, including some things you might expect - like statistics contents.

Context

StackExchange Database Administrators Q#166449, answer score: 4

Revisions (0)

No revisions yet.