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

  • SEARCH
  • Home
  • 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 3336544
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:08:53+00:00 2026-05-18T00:08:53+00:00

I have to write a SQL script to compare the differences between the indices

  • 0

I have to write a SQL script to compare the differences between the indices of two tables on SQL Server. How can I get the structure of the indices on a table through a SQL query?

  • 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-18T00:08:54+00:00Added an answer on May 18, 2026 at 12:08 am

    If the two databases are on the same server then you can do a series of queries that outer joins the sys.indexes and sys.index_columns tables on both databases. You will also need to look at sys.index_columns to check that the columns are the same (also check for the same order – this will affect query plans).

    If both databases are on different servers you will need to copy the contents of sys.indexes and sys.index_columns onto the other server and do a similar query against your copies of the tables.

    An example of such a query might look like (substitute appropriate databases for FOO and BAR and add appropriate filters to the individual queries if you want to look at individual tables):

    select *
      from
           (select s1.name           as SchemaName
                  ,t1.name           as TableName
                  ,c1.name           as ColumnName
                  ,i1.name           as IndexName
                  ,i1.index_id
                  ,c1.column_id
                  ,c1.system_type_id
                  ,c1.user_type_id
                  ,ty1.name          as ColumnType
                  ,c1.collation_name  -- Note this is nullable
                  ,c1.is_nullable
                  ,c1.max_length
                  ,c1.[precision]
                  ,c1.scale
                  ,ic1.index_column_id
                  ,ic1.key_ordinal
                  ,ic1.partition_ordinal
                  ,ic1.is_descending_key
                  ,ic1.is_included_column
              from [FOO].sys.schemas s1
              join [FOO].sys.tables t1
                on t1.schema_id = s1.schema_id
              join [FOO].sys.columns c1
                on t1.object_id = c1.object_id
              join [FOO].sys.types ty1
                on ty1.system_type_id = c1.system_type_id
               and ty1.user_type_id = c1.user_type_id
              join [FOO].sys.index_columns ic1
                on ic1.object_id = c1.object_id
               and ic1.column_id = c1.column_id
              join [FOO].sys.indexes i1
                on i1.object_id = ic1.object_id
               and i1.index_id = ic1.index_id) r1
      full outer join
           (select s1.name           as SchemaName
                  ,t1.name           as TableName
                  ,c1.name           as ColumnName
                  ,i1.name           as IndexName
                  ,i1.index_id
                  ,c1.column_id
                  ,c1.system_type_id
                  ,c1.user_type_id
                  ,ty1.name          as ColumnType
                  ,c1.collation_name  -- Note this is nullable
                  ,c1.is_nullable
                  ,c1.max_length
                  ,c1.[precision]
                  ,c1.scale
                  ,ic1.index_column_id
                  ,ic1.key_ordinal
                  ,ic1.partition_ordinal
                  ,ic1.is_descending_key
                  ,ic1.is_included_column
              from [BAR].sys.schemas s1
              join [BAR].sys.tables t1
                on t1.schema_id = s1.schema_id
              join [BAR].sys.columns c1
                on t1.object_id = c1.object_id
              join [BAR].sys.types ty1
                on ty1.system_type_id = c1.system_type_id
               and ty1.user_type_id = c1.user_type_id
              join [BAR].sys.index_columns ic1
                on ic1.object_id = c1.object_id
               and ic1.column_id = c1.column_id
              join [BAR].sys.indexes i1
                on i1.object_id = ic1.object_id
               and i1.index_id = ic1.index_id) r2 
        on r1.SchemaName = r2.SchemaName
       and r1.TableName = r2.TableName
       and r1.ColumnName = r2.ColumnName
       and r1.IndexName = r2.IndexName
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.