按列名获取索引

时间:2012-03-28 15:23:02

标签: sql sql-server

我正在使用Sql-Server,我有以下查询,它返回所有具有除“SQL_Latin1_General_CP1_CI_AS”以外的排序规则的列

select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS
where COLLATION_NAME <> 'SQL_Latin1_General_CP1_CI_AS'
and COLLATION_NAME is not null;

我还需要在这些列上获取索引,以便我可以删除这些索引,修改列,然后重新创建它们。

我的问题是我不知道如何获取上述查询返回的列的索引名称。

2 个答案:

答案 0 :(得分:2)

看看这对你有帮助:

select object_schema_name(object_id), object_name(object_id), name
from    sys.indexes
where   type > 0
and object_name(object_id) in (
    select C.column_name
    from INFORMATION_SCHEMA.COLUMNS C 
    where COLLATION_NAME is not null and COLLATION_NAME <> 'SQL_Latin1_General_CP1_CI_AS'
)

答案 1 :(得分:0)

试试这个:

select col.COLUMN_NAME,col.table_name,usg.CONSTRAINT_NAME as IndexName
from INFORMATION_SCHEMA.COLUMNS col
join INFORMATION_SCHEMA.KEY_COLUMN_USAGE usg
     on usg.TABLE_NAME=col.TABLE_NAME and usg.COLUMN_NAME=col.COLUMN_NAME
where col.COLLATION_NAME <> 'SQL_Latin1_General_CP1_CI_AS'
and col.COLLATION_NAME is not null;