Original: Deletes all indexes of the specified table, including primary key indexes, unique indexes, and normal indexes, for SQL Server 2005.
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
The
Deletes all indexes for the specified table, including primary key indexes, unique indexes, and normal indexes for SQL Server 2005.