5

我正在计划更改数据库,并且我有一个包含在进程中的列列表。我可以列出包含特定列的所有索引吗?

编辑

到目前为止,我有(结合答案):

declare @TableName nvarchar(128), @FieldName nvarchar(128)
select  @TableName= N'<<Table Name>>', @FieldName =N'<<Field Name>>'
(SELECT distinct systab.name AS TABLE_NAME,sysind.name AS INDEX_NAME, 'index' 
FROM sys.indexes sysind 
INNER JOIN sys.index_columns sysind_col 
    ON  sysind.object_id = sysind_col.object_id and sysind.index_id = sysind_col.index_id 
INNER JOIN sys.columns sys_col 
    ON sysind_col.object_id = sys_col.object_id and sysind_col.column_id = sys_col.column_id 
INNER JOIN sys.tables systab
    ON sysind.object_id = systab.object_id

WHERE systab.is_ms_shipped = 0 and sysind.is_primary_key=0 and sys_col.name  =@FieldName and systab.name=@TableName

union
select t.name TABLE_NAME,o.name, 'Default' OBJ_TYPE
 from sys.objects o 
inner join sys.columns c on o.object_id  = c.default_object_id
inner join sys.objects t on c.object_id  = t.object_id 
where o.type  in ('D') and c.name  =@FieldName and t.name=@TableName
union

SELECT u.TABLE_NAME,u.CONSTRAINT_NAME,  'Constraint' OBJ_TYPE  
FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE u 

where u.COLUMN_NAME  = @FieldName and u.TABLE_NAME = @TableName
) order by 1

但我对组合sys.和'INFORMATION_SCHEMA'不太满意。可以避免吗?

4

4 回答 4

4
---Using sp_helpindex and your TableName
exec sp_helpindex YourTableName

---Using sys.tables with your TableName and ColumnName
select distinct c.name, i.name, i.type_desc,...
from sys.indexes i
        join sys.index_columns ic on i.index_id = ic.index_id 
        join sys.columns c on ic.column_id = c.column_id
where i.object_id = OBJECT_ID(N'YourTableName') and c.name = 'YourColumnName'

编辑:根据评论,您也可以在不使用的情况下加入 object_Idsdistinct

select c.name, i.name, i.type_desc
from sys.indexes i
        join sys.index_columns ic on i.index_id = ic.index_id and i.object_id = ic.object_id
        join sys.columns c on ic.column_id = c.column_id and ic.object_id = c.object_id
where i.object_id = OBJECT_ID(N'YourTableName') and c.name = 'YourColumnName'
于 2013-10-04T09:15:52.940 回答
3

SELECT * FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE

如果您知道表的约束条件,请在 WHERE 中使用 TABLE_NAME。

如果您知道列名,请使用 column_name。

于 2013-10-04T08:19:15.893 回答
2

要获取有关包含特定列的所有索引的信息,可以使用以下两个目录视图:

sys.indexes , sys.index_columns

询问:

SELECT 
     sysind.name AS INDEX_NAME
    ,sysind.index_id AS  INDEX_ID
    ,sys_col.name AS COLUMN_NAME
    ,systab.name AS TABLE_NAME
FROM sys.indexes sysind 
INNER JOIN sys.index_columns sysind_col 
    ON  sysind.object_id = sysind_col.object_id and sysind.index_id = sysind_col.index_id 
INNER JOIN sys.columns sys_col 
    ON sysind_col.object_id = sys_col.object_id and sysind_col.column_id = sys_col.column_id 
INNER JOIN sys.tables systab
    ON sysind.object_id = systab.object_id 
WHERE (1=1) 
      AND systab.is_ms_shipped = 0 
      AND sys_col.name  IN(specific column list for which indexes are to be queried)
ORDER BY 
    systab.name,sys_col.name, sysind.name,sysind.index_id

希望这可以帮助!

于 2013-10-04T09:07:46.190 回答
0

如果您有权访问数据库上的 sys 架构,则可以查询sys.indexes以获取索引 ID,并将其与函数一起使用index_col以获取列。for 的最后一个参数index_col是索引中列的索引(即,如果索引中有 3 列,则必须index_col在最后一个参数中使用 1、2、3 调用 3 次)

select index_id from sys.indexes
 where object_id = object_id(@objectname)

select index_col(@objectname, @indexid, 1)

这应该给你约束,因为它们只是特殊的索引。

于 2013-10-04T09:07:11.360 回答