Disable all non-clustered indexes

后端 未结 5 2020
星月不相逢
星月不相逢 2020-12-29 05:36

I select a number of non-clustered indexes from my database with the following:

SELECT  sys.objects.name tableName,
        sys.indexes.name indexName
FROM           


        
5条回答
  •  悲哀的现实
    2020-12-29 06:26

    You can build the queries into a select statement, like so:

    DECLARE @sql AS VARCHAR(MAX)='';
    
    SELECT @sql = @sql + 
    'ALTER INDEX ' + sys.indexes.name + ' ON  ' + sys.objects.name + ' DISABLE;' +CHAR(13)+CHAR(10)
    FROM 
        sys.indexes
    JOIN 
        sys.objects 
        ON sys.indexes.object_id = sys.objects.object_id
    WHERE sys.indexes.type_desc = 'NONCLUSTERED'
      AND sys.objects.type_desc = 'USER_TABLE';
    
    EXEC(@sql);
    

    Chars 13 and 10 are the line-feed/carriage-returns, so you can check the output by replacing EXEC with PRINT, and it will be more readable.

提交回复
热议问题