就像tittle暗示的那样,我正在尝试删除一个索引,但它不起作用,即使删除约束也是如此。使用的代码如下:
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] Msg 3701, 等级11,状态7,行1不能删除索引 'dbo.TB_CARTAO_CREDITO.IX_TB_CARTAO_CREDITO_UNIQUE',因为它确实 不存在或您没有许可。
谢谢和问候。
编辑:
所以,这些是我在Hamlet 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
答案 0 :(得分:4)
错误信息非常清楚。当你放弃约束时,相应的对象(即索引)也将被删除。