SQL存储过程之删除指定表中所有索引

--删除指定表中所有索引
--用法:declare @tableName varchar(100) 
--set @tableName='表名' --表名 ,根据实际情况替换
--exec sp_dropindex @tableName
if exists(select 1 from sysobjects where id = object_id('dropindex') and xtype = 'P') 
drop procedure dropindex 
go

create procedure dropindex @tableName varchar(100)=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 
 if exists(select 1 from sysobjects A join # B on A.name=B.index_name where B.id=@i 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 id=@i) 
 end 
 
-- print(@sql) 
    exec(@sql) 
 set @i=@i+1 
end

drop table #

go


先执行上面的SQL语句,然后再执行此存储过程即可

--删除索引
declare @tableName varchar(100) 
set @tableName='table' --表名 ,根据实际情况替换
exec dropindex @tableName
GO


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值