Skip to main content

Command Palette

Search for a command to run...

Script Indexes With Your Tables

Updated
1 min read
Script Indexes With Your Tables
B

Bradley Schacht is a Principal Program Manager on the Microsoft Fabric product team based in Saint Augustine, FL. Bradley is a former consultant, trainer, and has coauthored 6 SQL Server and Power BI books, most recently Learn Microsoft Fabric. As a member of the Microsoft Fabric product team, Bradley works directly with customers to solve some of their most complex data problems and helps shape the future of Microsoft Fabric. Bradley gives back to the community through speaking at events such as the SQLBits, Fabric Community Conference, PASS Community Data Summit, SQL Saturdays, Code Camps, and user groups across the country including locally at the Jacksonville SQL Server User Group (JSSUG). He is a contributor on SQLServerCentral.com and blogs on his personal site, BradleySchacht.com.

Scripting tables inside of SQL Server Management Studio is a very simple task. Just right click and select script table. The problem is that, but default, the indexes on a table are not part of the script. Rather than scripting the indexes separately from the table you can tell management studio that you want the indexes included. Click on Tools then Options and expand the SQL Server Object Explorer section on the left side. From there select Scripting and scroll down to the section for "Table and view options". There are numerous options under this section for scripting tables. The one we are interested in this case is the "Script indexes" setting; this is False by default. Just change it to true and now whenever you right click and script a table it will include all index, clustered and non-clustered, in the create table script. Some other usefull settings (some of which are enabled by default that you may want to turn off at some point) are: *Include IDENTITY property *Schema qualify foreign key references *Script CHECK constraints *Script defaults *Script foreign keys Script full-text indexes *Script primary keys Script statistics Script triggers NOTE: A * before the item denotes that it is enabled by default

More from this blog

B

Bradley Schacht - Development

172 posts

Bradley Schacht is a Principal Program Manager on the Microsoft Fabric product team based in Saint Augustine, FL.