sql 删除所有非聚集索引 - SQL Server

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 删除所有非聚集索引 - SQL Server相关的知识,希望对你有一定的参考价值。

IF EXISTS
  (SELECT *
   FROM [tempdb].[dbo].sysobjects
   WHERE id = Object_id(N'[tempdb].[dbo].[##NonClusteredIndexesToDelete]') )
DROP TABLE ##nonclusteredindexestodelete
CREATE TABLE ##nonclusteredindexestodelete ([name] SYSNAME,
                                            [tablename] SYSNAME)
INSERT ##nonclusteredindexestodelete
SELECT sysindexes.[name],
       sysobjects.[name] AS TableName
FROM sysindexes(nolock)
INNER JOIN sysobjects ON sysindexes.id = sysobjects.id
WHERE (indid > 1 )-- delete only non clustered indexes

  AND (sysindexes.[name] NOT LIKE '_WA%' )-- don't delete statistics  
 
  AND (sysobjects.[type] != 'S' )-- don't delete system indexes

  AND (sysobjects.[xtype] != 'IT' )-- don't delete system indexes  
 
  AND (NOT (sysindexes.[status] = 4098
            AND sysindexes.indid = 2) )-- don't delete unique indexes
ORDER BY (sysindexes.[name]) DECLARE @name SYSNAME DECLARE @TableName SYSNAME DECLARE @sql VARCHAR(4096) DECLARE vcursor
CURSOR
FOR
SELECT [name],
       [tablename]
FROM ##nonclusteredindexestodelete
ORDER BY [name] OPEN vcursor FETCH NEXT
FROM vcursor INTO @name,
                  @TableName WHILE @@FETCH_STATUS = 0 BEGIN
SET @sql = 'drop index ' + @TableName + '.' + @name EXEC (@sql) FETCH NEXT
FROM vcursor INTO @name,
                  @TableName END CLOSE vcursor DEALLOCATE vcursor
DROP TABLE ##nonclusteredindexestodelete

以上是关于sql 删除所有非聚集索引 - SQL Server的主要内容,如果未能解决你的问题,请参考以下文章

SQL索引一步到位

索引的访问-SQL Server

SQL Server 非聚集索引

SQL SERVER大话存储结构

SQL SERVER数据库 唯一索引 非唯一索引 聚集索引 非聚集索引 之间区别

sql server 索引总结三