Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7403435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:04:52+00:00 2026-05-29T05:04:52+00:00

I want to generate a script of all the indexes in a database(Create Index).

  • 0

I want to generate a script of all the indexes in a database(Create Index).

I don’t want the index script along with create table script. how can we generate it in SQL Server (2005 or 2008). There needs to be separate script for Clustered and Non Clustered Indexes.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:04:53+00:00Added an answer on May 29, 2026 at 5:04 am

    This is an excellent article on SQL Server Central that I’ve used.

    Here is another that you might like better.

    If it’s not exactly what you’re looking for (sorting, filtering) I’m sure it would be easier to edit these than to start from scratch.

    Here’s the slightly corrected (2019) code from the first example:

    -- Get all existing indexes, but NOT the primary keys
    DECLARE cIX CURSOR FOR
        SELECT OBJECT_NAME(SI.Object_ID), SI.Object_ID, SI.Name, SI.Index_ID
            FROM Sys.Indexes SI 
                LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC ON SI.Name = TC.CONSTRAINT_NAME AND OBJECT_NAME(SI.Object_ID) = TC.TABLE_NAME
            WHERE TC.CONSTRAINT_NAME IS NULL
                AND OBJECTPROPERTY(SI.Object_ID, 'IsUserTable') = 1
            ORDER BY OBJECT_NAME(SI.Object_ID), SI.Index_ID
    
    DECLARE @IxTable SYSNAME
    DECLARE @IxTableID INT
    DECLARE @IxName SYSNAME
    DECLARE @IxID INT
    
    -- Loop through all indexes
    OPEN cIX
    FETCH NEXT FROM cIX INTO @IxTable, @IxTableID, @IxName, @IxID
    WHILE (@@FETCH_STATUS = 0)
    BEGIN
        DECLARE @IXSQL NVARCHAR(4000) SET @IXSQL = ''
        SET @IXSQL = 'CREATE '
    
        -- Check if the index is unique
        IF (INDEXPROPERTY(@IxTableID, @IxName, 'IsUnique') = 1)
            SET @IXSQL = @IXSQL + 'UNIQUE '
        -- Check if the index is clustered
        IF (INDEXPROPERTY(@IxTableID, @IxName, 'IsClustered') = 1)
            SET @IXSQL = @IXSQL + 'CLUSTERED '
    
        SET @IXSQL = @IXSQL + 'INDEX ' + @IxName + ' ON ' + @IxTable + '('
    
        -- Get all columns of the index
        DECLARE cIxColumn CURSOR FOR 
            SELECT SC.Name
            FROM Sys.Index_Columns IC
                JOIN Sys.Columns SC ON IC.Object_ID = SC.Object_ID AND IC.Column_ID = SC.Column_ID
            WHERE IC.Object_ID = @IxTableID AND Index_ID = @IxID
            ORDER BY IC.Index_Column_ID
    
        DECLARE @IxColumn SYSNAME
        DECLARE @IxFirstColumn BIT SET @IxFirstColumn = 1
    
        -- Loop throug all columns of the index and append them to the CREATE statement
        OPEN cIxColumn
        FETCH NEXT FROM cIxColumn INTO @IxColumn
        WHILE (@@FETCH_STATUS = 0)
        BEGIN
            IF (@IxFirstColumn = 1)
                SET @IxFirstColumn = 0
            ELSE
                SET @IXSQL = @IXSQL + ', '
    
            SET @IXSQL = @IXSQL + @IxColumn
    
            FETCH NEXT FROM cIxColumn INTO @IxColumn
        END
        CLOSE cIxColumn
        DEALLOCATE cIxColumn
    
        SET @IXSQL = @IXSQL + ')'
        -- Print out the CREATE statement for the index
        IF @IXSQL != '' BEGIN PRINT @IXSQL END
    
        FETCH NEXT FROM cIX INTO @IxTable, @IxTableID, @IxName, @IxID
    END
    
    CLOSE cIX
    DEALLOCATE cIX
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL Server database for which I want to generate script of
I'm using SQL Server 2005 and I want to export all records from my
i want to generate the Create Table Script for a specific entity Class. I
I'm using SQL Server 2008. I'm trying to script out all my stored procedures,
I am trying to alter a table in SQL server with a script. In
I want to generate an HTML table from a couple specified parameters. Specifically, the
I want to generate all variations with repetitions of a string in C++ and
I'd like to automate the script generation in SQL Server Management Studio 2008. Right
Our SQL Server 2000 instance hosts several databases which are all similar, one for
I have a Visual Studio 2010 Database project, from which I want to generate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.