?
Solved

Generate script to create all Indexes, keys, defaults

Posted on 2011-03-15
1
Medium Priority
?
678 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are some very powerful Dynamic Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a di…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

765 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