views:

125

answers:

2

Hi All,

Is there a system SP or dmv that allows me to select indexes in my DB based on columns and table names?

What I am trying to do is to drop indexes based on columns with string datatypes as I am in the process of altering my collation settings.

Thanks.

+2  A: 

You can always determine the indices for a given column and/or table by querying the sys.indexes and sys.index_columns views:

SELECT
    i.Name 'Index Name',
    OBJECT_NAME(i.object_ID) 'Table Name',
    c.Name 'Column Name'
FROM  
   sys.indexes i 
INNER JOIN 
   sys.index_columns ic ON i.index_id = ic.index_id AND i.object_id = ic.object_id
INNER JOIN
   sys.columns c ON ic.column_id = c.column_id AND ic.object_id = c.object_id
WHERE 
  c.name = 'Index column name'      -- this for a column name
  -- OBJECT_NAME(i.OBJECT_ID) = 'Table Name'   -- this for table name

Marc

marc_s
brilliant, thanks!
Nai
+1  A: 

you need four system views:

sys.indexes, sys.index_columns, sys.columns and sys.objects. sys.objects contains the name of table, sys.columns. contains object_id of table and type of column, sys.index_columns contains columns id that are present in indexes and index id, sys.indexes contains index id.

edit: yes, sys.objects is not necessary. Tanks @mark.

Alex_L