天天看点

删除指定表的所有索引,包括主键索引,唯一索引和普通索引 ,适用于sql server 2005 .

删除指定表的所有索引,包括主键索引,唯一索引和普通索引 ,适用于sql server 2005,

使用说明 :

1,先执行脚本,将存储过程创建在数据库中

2,调用方法,以黄金搭档数据库为例

use velcromfm --数据库名, 根据具体项目替换

go

declare @tableName varchar(20)

set @tableName='menu' --表名 ,根据实际情况替换

exec sp_dropindex @tableName

3,如有需要,该脚本稍加改写就可以做成一个删除数据库所有索引的脚本

提供一个思路 :

select * from sysobjects where xtype='u' --查除数据库里所有的表,使用游标遍历执行这个存储过程

4,该脚本有个问题,如果某个pk索引是另外一个表的外键的话就删不掉,但在velcro系统的数据库不会有这个麻烦,应为我们的系统没有设任何外键^-^

脚本如下

*/

----------------------------- 脚本---------------------------------------------------

if exists(select 1 from sysobjects where id = object_id('sp_dropindex') and xtype = 'P')

drop procedure sp_dropindex

create procedure sp_dropindex @tableName varchar(50)=null --表名

as

if @tableName is null

begin

 raiserror('必须提供@tableName参数',12,1)

 return

end

create 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 @tableName

declare @i int

declare @sql varchar(100)

set @i = 1

while @i<=(select max(id) from #)

 begin

  select @sql = 'alter table '+ @tableName +' drop constraint ' + (select index_name from # where id = @i)

 end

 else

-- print(@sql)

    exec(@sql) 

 set @i=@i+1

drop table #