Question : drop all indexes

when i get the script to remove all indexes, still it fails due to FK constraints. how will you modify the below code to accommodate for all indexes and any constraints blocking it?

thanks
1:
2:
3:
4:
5:
6:
7:
8:
SELECT
CASE  
    WHEN (is_primary_key =1 or is_unique = 1)
    THEN 'ALTER TABLE ['+SCHEMA_NAME(o.uid)+'].['+o.name+'] DROP CONSTRAINT ['+i.name+']'
    ELSE 'DROP INDEX [' +i.name +'] ON ['+schema_name(o.uid)+'].['+i.name+']'
    END 
FROM sys.indexes i join sysobjects o
ON i.object_id=o.id and i.object_id>99 and i.type_desc<>'HEAP' and o.type ='U'

Answer : drop all indexes

The problem is not so much with drop index; the problem is with drop (primary key/unique key) constraint.  You cannot drop such a constraint if there is a FK constraint depending on it.

When dropping ALL indexes, it's pretty straight-forward to drop ALL FK constraint's before dropping ANY PK or UK constraints.

When dropping all indexes for a single table, then you need a cursor or recursive CTE to drop all the FK's that reference that table before dropping any PK or UK constraints.

When dropping a single index, then you need a cursor or recursive CTE to drop all the FK's that depend on that index before dropping any PK or UK constraints.

 

Random Solutions  
 
programming4us programming4us