標籤:des blog http io ar os sp div on
原文:刪除指定表的所有索引,包括主鍵索引,唯一索引和普通索引 ,適用於sql server 2005,
--刪除指定表中所有索引--用法:declare @tableName varchar(100) --set @tableName=‘表名‘ --表名 ,根據實際情況替換--exec sp_dropindex @tableNameif exists(select 1 from sysobjects where id = object_id(‘dropindex‘) and xtype = ‘P‘) drop procedure dropindex gocreate procedure dropindex @tableName varchar(100)=null --表名asif @tableName is null begin raiserror(‘必須提供@tableName參數‘,12,1) return endcreate table # ( id int identity, index_name varchar(50), index_description varchar(1000), index_keys varchar(100) )insert #(index_name,index_description,index_keys) exec sp_helpindex @tableNamedeclare @i intdeclare @sql varchar(100) set @i = 1while @i<=(select max(id) from #) begin if exists(select 1 from sysobjects A join # B on A.name=B.index_name where [email protected] and A.xtype in (‘PK‘,‘UQ‘)) begin select @sql = ‘alter table ‘+ @tableName +‘ drop constraint ‘ + (select index_name from # where id = @i) end else begin select @sql = ‘drop index ‘+ @tableName + ‘.‘ + (select index_name from # where [email protected]) end -- print(@sql) exec(@sql) set @[email protected]+1 enddrop table #go
先執行上面的SQL語句,然後再執行此預存程序即可
--刪除索引declare @tableName varchar(100) set @tableName=‘table‘ --表名 ,根據實際情況替換exec dropindex @tableNameGO
刪除指定表的所有索引,包括主鍵索引,唯一索引和普通索引 ,適用於sql server 2005,