像丝毫不差暗示,我试图删除一个指标,但它不工作,消除约束时也是如此。 使用的代码是以下几点:
OPEN cursor_drop_indexes
FETCH next FROM cursor_drop_indexes INTO @Indexes_SchemaName, @Indexes_TableName, @Indexes_TableId,
@Indexes_IndexName, @Indexes_IndexId, @Indexes_FileGroupName, @Indexes_FillFactor
WHILE ( @@Fetch_Status = 0 )
BEGIN
-- Used to remove UNIQUE KEY constraint enforcements
IF @Indexes_IndexName NOT LIKE '%missing%' BEGIN
SET @sql_index = ' ALTER TABLE [' + @Indexes_TableName + '] DROP CONSTRAINT ['
+ @Indexes_IndexName + ']'
PRINT( @sql_index )
EXEC sp_executesql @sql_index
END
-- Remove Index
SET @sql_index = ' DROP INDEX [' + @Indexes_IndexName + '] ON ['
+ @Indexes_SchemaName + '].[' + @Indexes_TableName + ']'
PRINT( @sql_index )
EXEC sp_executesql @sql_index
FETCH next FROM cursor_drop_indexes INTO @Indexes_SchemaName, @Indexes_TableName, @Indexes_TableId
,
@Indexes_IndexName, @Indexes_IndexId, @Indexes_FileGroupName, @Indexes_FillFactor
END
CLOSE cursor_drop_indexes
和错误消息是:
ALTER TABLE [TB_CARTAO_CREDITO] DROP CONSTRAINT [IX_TB_CARTAO_CREDITO_UNIQUE] DROP INDEX [IX_TB_CARTAO_CREDITO_UNIQUE] ON [DBO]。[TB_CARTAO_CREDITO]消息3701,级别11,状态7,1行无法删除索引 'dbo.TB_CARTAO_CREDITO.IX_TB_CARTAO_CREDITO_UNIQUE',因为它不存在,或者您没有权限。
谢谢并恭祝安康。
编辑:
所以,这些都是我的代码作出后哈姆雷特Hakobyan帮我明显的变化
IF (SELECT is_unique FROM SYS.indexes WHERE name = @Indexes_IndexName ) = 1 BEGIN
SET @sql_index = ' ALTER TABLE [' + @Indexes_TableName + '] DROP CONSTRAINT ['
+ @Indexes_IndexName + ']'
END
ELSE
-- Remove Index
SET @sql_index = ' DROP INDEX [' + @Indexes_IndexName + '] ON ['
+ @Indexes_SchemaName + '].[' + @Indexes_TableName + ']'
PRINT( @sql_index )
EXEC sp_executesql @sql_index