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?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
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):