Sunday, April 15, 2007

Drop all indexes

Well, sometimes we have to do that. A large INSERT statement may run faster for instance. The below script just generate DROP command of all indexes in current dataabse. You can add a filter to specify a table name for example.(SQL Server 2000)

SELECT
'DROP INDEX ' +
QUOTENAME(USER_NAME(o.uid)) +
'.' +
QUOTENAME(o.name) +
'.' +
QUOTENAME(i.name)
FROM sysobjects o
JOIN sysindexes i ON
i.id = o.id
WHERE i.indid BETWEEN 1 AND 254 AND
INDEXPROPERTY(i.id, i.name, 'IsStatistics') = 0 AND
INDEXPROPERTY(i.id, i.name, 'IsHypothetical') = 0 AND
OBJECTPROPERTY(o.id, 'IsMSShipped') = 0

3 comments:

Walking on Sea said...

The script worked just fine. Thanks.

But there is one issue. i am getting the following error and thus unable to delete. any solution for it. I mean for all theindexes in the db.

"An explicit DROP INDEX is not allowed on index . It is being used for PRIMARY KEY constraint enforcement."

aiya said...
This comment has been removed by a blog administrator.
aiya said...
This comment has been removed by a blog administrator.