Deletes all indexes of the specified table, including primary key indexes, unique indexes, and normal indexes, for SQL Server 2005,

Source: Internet
Author: User

Original: Deletes all indexes of the specified table, including primary key index, unique index, and normal index, for SQL Server 2005,

--Delete all indexes in the specified table--usage: DECLARE @tableName varchar--set @tableName = ' table name '-The table name, replacing--exec Sp_dropindex according to the actual situation @tableNameif Exists (select 1 from sysobjects where id = object_id (' Dropindex ') and xtype = ' P ') drop procedure Dropindex gocreate Proce Dure dropindex @tableName varchar (=null)--table name Asif @tableName is null begin RAISERROR (' must provide @tablename parameter ', 12,1) return Endcreate table # (ID int identity, index_name varchar (), index_description varchar (+), 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 constr Aint ' + (select Index_name from # where id = @i) end ELSE BEGIN Select @sql = ' Drop index ' + @tableName + '. ' + (sel ECT index_name from # WHERE [emAil protected]) End--Print (@sql) exec (@sql) Set @[email protected]+1 enddrop table #go 

Execute the preceding SQL statement before executing this stored procedure.

--Delete Index declare @tableName varchar set @tableName = ' table '-the name of the table, replacing the exec Dropindex according to the actual situation @tableNameGO

Deletes all indexes of the specified table, including primary key indexes, unique indexes, and normal indexes, for SQL Server 2005,

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.