Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate CREATE INDEX statements in SQL Server

Does anyone have a script to list of CREATE INDEX statements for all existing indexes in a SQL Server database?

This thread List of all index & index columns in SQL Server DB has great tips on how to find them. But a script to generate the CREATE INDEX statements would be great. Sometimes we come into a situation without adequate data, or indexes have been added in an ad-hoc manner over time without documentation, so the create statements are missing. Like in a situation I find myself in right now.

Thanks.

like image 950
royappa Avatar asked Oct 01 '12 11:10

royappa


People also ask

Can I CREATE INDEX on Create Table statement?

It is possible to create a primary key or unique index within a SQL Server CREATE TABLE statement. Is it possible to create a non-unique index within a CREATE TABLE statement? Again, the goal is to create the non-unique index within the CREATE TABLE statement, not after it.

Does SQL Server automatically create indexes?

A unique index is automatically created when you define a primary key or unique constraint: Primary key: When you define a primary key constraint on one or more columns, SQL Server automatically creates a unique, clustered index if a clustered index does not already exist on the table or view.

How do I create an index on multiple columns in SQL Server?

SQL Server CREATE INDEX statement In this syntax: First, specify the name of the index after the CREATE NONCLUSTERED INDEX clause. Note that the NONCLUSTERED keyword is optional. Second, specify the table name on which you want to create the index and a list of columns of that table as the index key columns.


1 Answers

Use Generate Scripts from SQL Management Studio and choose the "Script Indexes" options (under Advanced Scripting options)

like image 133
podiluska Avatar answered Oct 22 '22 13:10

podiluska