2012-11-15 23 views
7

我试图找到构建在数据类型uniqueIdentifier的列上的所有聚簇索引。显然这是一个聚集索引的可怕选择,我试图找到所有这些并将其删除。我迄今为止编写的脚本为每个具有uniqueIdentifier的表返回所有聚簇索引。请帮忙。下面是该脚本:SQL Server的脚本来查找建立在特定数据类型上的所有聚簇索引

select distinct object_name(i.object_id) AS tablename, i.name AS indexname, i.type_desc  as type 
from sys.indexes i 
join sys.index_columns ic on ic.object_id = i.object_id and ic.index_id = i.index_id 
join sys.columns c on c.column_id = ic.index_column_id 
join sys.types t on t.system_type_id = c.system_type_id 
join sys.objects o on o.object_id = i.object_id 
where t.name = 'uniqueidentifier' 
and i.type_desc = 'clustered' 
and object_name(i.object_id) not like 'sys%' 
+0

它看起来像你的丢失位为ON子句SYS.COLUMNS,其中应包括OBJECT_ID – RichardTheKiwi

+0

这是一个可怕的选择** **如果值被随机分配(如通过'newid()')。如果分配顺序值,它不一定是一个可怕的选择。 –

回答

8
select o.name objectname, i.name indexname, c.name as columnname 
from sys.objects o 
join sys.indexes i on i.object_id = o.object_id 
join sys.index_columns ic on ic.index_id = i.index_id and ic.object_id = i.object_id 
join sys.columns c on c.object_id = o.object_id and c.column_id = ic.column_id 
join sys.types t on c.system_type_id = t.system_type_id 
where o.is_ms_shipped = 0 
    and i.type_desc = 'CLUSTERED' 
    and t.name = 'uniqueidentifier' 
order by o.name, i.name 
+0

谢谢RichardTheKiwi。这工作奇妙:) – user1825469

+0

不客气。请记得通过打勾来接受答案。 – RichardTheKiwi

相关问题