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

Source: Internet
Author: User

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

Instructions for use:

1. Execute the script first to create the stored procedure in the database

2, call the method, take the gold partner database as an example

Use VELCROMFM--database name, replace with specific item

Go

DECLARE @tableName varchar (20)
Set @tableName = ' Menu '-table name, replace according to actual situation
EXEC Sp_dropindex @tableName

3, if necessary, the script can be made a little rewrite to make a script to delete all the indexes of the database

Provide an idea:

SELECT * from sysobjects where xtype= ' u '--check all tables in the database, use cursor traversal to execute this stored procedure

4, the script has a problem, if a PK index is a foreign key of another table can not be deleted, but in the Velcro system of the database will not have this trouble, should be for our system does not set any foreign key ^-^

The script is as follows

*/

-----------------------------Script---------------------------------------------------


if exists (select 1 from sysobjects where id = object_id (' Sp_dropindex ') and xtype = ' P ')
drop procedure Sp_dropindex
Go

CREATE PROCEDURE Sp_dropindex @tableName varchar (=null)--table name
As

If @tableName is null
Begin
RAISERROR (' must provide @tablename parameter ', 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 joins # B on A.name=b.index_name where [e-mail 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
End

DROP TABLE #

Go

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.