刪除指定表的所有索引

2021-04-01 20:26:58 字數 1329 閱讀 6932

刪除指定表的所有索引

/*刪除指定表的所有索引,包括主鍵索引,唯一索引和普通索引

呼叫:declare @tbname varchar(20)

set @tbname='a'

exec sp_dropindex @tbname

vivianfdlpw 2005.9 引用情保留此資訊

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

drop procedure sp_dropindex

gocreate procedure sp_dropindex

@tbname varchar(20)=null   --索引名

asif @tbname is null

begin

raiserror('必須提供@tbname引數',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 @tbname

declare @i int,@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 '+@tbname+' drop constraint '

+(select index_name from # where id=@i)

exec(@sql)

endelse

begin

select @sql='drop index '+@tbname+'.'

+(select index_name from # where id=@i)

exec(@sql)

endset @i=@i+1

enddrop table #

go

刪除指定表的所有索引

刪除指定表的所有索引,包括主鍵索引,唯一索引和普通索引 呼叫 declare tbname varchar 20 set tbname a exec sp dropindex tbname vivianfdlpw 2005.9 引用情保留此資訊 if exists select 1 from sys...

刪除指定表的所有索引

刪除指定表的所有索引 刪除指定表的所有索引,包括主鍵索引,唯一索引和普通索引 呼叫 declare tbname varchar 20 set tbname a exec sp dropindex tbname vivianfdlpw 2005.9 引用情保留此資訊 if exists select...

oracle刪除指定使用者所有表

1 select drop table table name from all tables where owner 要刪除的使用者名稱 注意要大寫 2 刪除所有表 以使用者test為例 for example declare cursor cur1 is select table name fro...

生成指定表rebuild所有索引的語句

需要對錶大資料量操作的時候,如delete,需要對索引可以選擇性的操作!可以使用下面語句生成 declare tname varchar 100 declare size int set size 0 這裡設定索引大小限制,如果不設定預設為0即所有索引 set tname tblorders sel...

SQL刪除表名包含指定字元的所有表

create procedure dbo.proc deltable as declare tablename as nvarchar 128 declare cur del cursor for 以下有3種不同的篩選方式,反註釋即可使用 刪除表名以kcpd開頭的所有表 kcpd select na...