下面是代码段,它将显示重复索引,第二个查询将返回相同的脚本。你只需要提供表名。对于实施例 组@tablename =“SalaryMaster” 这可以通过使用完成while循环上
select name from sys.tables
放入临时表应用而循环,并执行下面的代码
declare @TableName as varchar(50)
set @TableName = 'EmployeeMaster'
--- 1.
select name as IndexName,Index_id as IndexID from sys.indexes
where index_id NOT in (
select MIN(index_id) from (
select avg(column_id) avgCol,index_id from sys.index_columns where
object_id = object_id(@TableName)
group by index_id
) as a group by avgCol
) and object_id = object_id(@TableName)
--- 2.
select '
IF EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'''[email protected]+''')
AND name = N'''+name+''')
DROP INDEX ['+name+'] ON '[email protected]+' WITH (ONLINE = OFF)
' from sys.indexes
where index_id NOT in (
select MIN(index_id) from (
select avg(column_id) avgCol,index_id from sys.index_columns where
object_id = object_id(@TableName)
group by index_id
) as a group by avgCol
) and object_id = object_id(@TableName)
整理了在制备用于迁移,但你已经有两个体面的答案。你可以试试吗?如果您有可接受的答案,我宁愿不迁移。 – Will
@威尔:非常感谢我已经尝试了他们两个。但我仍在等待完美的答案。谢谢。 –