[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Generate script to create all Indexes, keys, defaults

Posted on 2011-03-15
1
Medium Priority
?
679 Views
Last Modified: 2012-05-11
Hi,

I would like to create a script that will generate for all non-clustered indexes, unique indexes in a database. I know with generate script you can do for all tables, but i just want these individually.
0
Comment
Question by:Auerelio Vasquez
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 4

Accepted Solution

by:
qasim_md earned 2000 total points
ID: 35140574
Try this and let me know if it was helpful.Please change the collation name in this SQL, as per your DB collation...

WITH indexCTE AS
(
    SELECT DISTINCT
        i.index_id, i.name, i.object_id
    FROM
        sys.indexes i
    INNER JOIN
        sys.index_columns ic
           ON i.index_id = ic.index_id AND i.object_id = ic.object_id
    WHERE
        EXISTS (SELECT * FROM sys.columns c
                 WHERE c.collation_name = 'SQL_Latin1_General_CP1_CI_AS'
                 AND c.column_id = ic.column_id AND c.object_id = ic.object_id)
),
indexCTE2 AS
(
    SELECT
        indexCTE.name 'IndexName',
        OBJECT_NAME(indexCTE.object_ID) 'TableName',
        CASE indexCTE.index_id
          WHEN 1 THEN 'CLUSTERED'
          ELSE 'NONCLUSTERED'
        END AS 'IndexType',
        (SELECT DISTINCT c.name + ','
         FROM
            sys.columns c
         INNER JOIN
            sys.index_columns ic
               ON c.object_id = ic.object_id AND ic.column_id = c.column_id AND ic.Is_Included_Column = 0
         WHERE
            indexCTE.OBJECT_ID = ic.object_id
            AND indexCTE.index_id = ic.index_id
         FOR XML PATH('')
        ) ixcols,
        ISNULL(
        (SELECT DISTINCT c.name + ','
         FROM
            sys.columns c
         INNER JOIN
            sys.index_columns ic
               ON c.object_id = ic.object_id AND ic.column_id = c.column_id AND ic.Is_Included_Column = 1
         WHERE
            indexCTE.OBJECT_ID = ic.object_id
            AND indexCTE.index_id = ic.index_id
         FOR XML PATH('')
        ), '') includedcols
    FROM
        indexCTE
)
SELECT
    'CREATE ' + IndexType + ' INDEX ' + IndexName + ' ON ' + TableName +
        '(' + SUBSTRING(ixcols, 1, LEN(ixcols)-1) +
        CASE LEN(includedcols)
          WHEN 0 THEN ')'
          ELSE ') INCLUDE (' + SUBSTRING(includedcols, 1, LEN(includedcols)-1) + ')'
        END
FROM
   indexCTE2
ORDER BY
   TableName, IndexName
 
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question