Answers:
您可以使用sys.indexes视图获取索引。您可以将此表连接到sys.tables,sys.columns和sys.index_column以获得创建动态删除索引的信息。您可以使用此简单选择来生成您的放置索引。您可以使用where子句来过滤表和列。如果要更改Table1.Column1,则必须使用这些名称来过滤select以获取正确的delete语句
use [testdb]
go
declare @sqlDropIndex NVARCHAR(1000)
select @sqlDropIndex = 'DROP INDEX ' + idx.name + ' ON ' + tbl.name
from sys.indexes idx inner join
sys.tables tbl on idx.object_id = tbl.object_id inner join
sys.index_columns idxCol on idx.index_id = idxCol.index_id inner join
sys.columns col on idxCol.column_id = col.column_id
where idx.type <> 0 and
tbl.name = 'MyTableName' and
col.name = 'MyColumnName'
group by idx.name, tbl.name
order by idx.name desc
print @sqlDropIndex
--exec sp_executeSql @sqlDropIndex
go
希望对你有帮助
Niko的答案有2个错误(缺少2个object_id条件)。无需group by
:
SELECT @sql = 'DROP INDEX ' + idx.name + ' ON ' + tbl.name
FROM sys.indexes idx
INNER JOIN sys.tables tbl ON idx.object_id = tbl.object_id
INNER JOIN sys.index_columns idxCol ON idx.index_id = idxCol.index_id AND idx.object_id = idxCol.object_id
INNER JOIN sys.columns col ON idxCol.column_id = col.column_id AND idxCol.object_id = col.object_id
WHERE idx.type <> 0
AND tbl.name = 'file_transfer_log'
AND col.name = 'tender_id';